/* Biostat 200 Lab 1 This lab will introduce some basic commands to * inspect a dataset * do some basic data management * demonstrate use of do-file storing your commands, and * make a log file to capture the output. The do-files we provide for these labs use a bit more than the most basic Stata programming, for the benefit of students who want to learn it. For example, * loops, for performing essentially the same command repeatedly with minor changes to the inputs * local and global variables to store a number or a string, which can then be plugged in elsewhere as needed. If this is not of interest, you can simply run the code we provide, focusing on understanding the result. To use this do-file, we suggest that you highlight the commands, or blocks of commands, between each block of comments and instructions, then press the do button in the upper right corner of the do-file editor. In some cases, it is crucial to run a whole block of code containing a loop together for it to work, starting from the line with a foreach or forvalues command, and running through the closing bracket }, which is on a separate line. /* Keeping a log file is a good way to record the results of the do-file. We first make sure there is no existing log file open, then start a new one. "cap" is short for capture and prevents Stata from stopping if there is no log file open. This is handy when you are running a do-file repeatedly to debug it. */ capture log close log using "lab 1", replace /* Read in the data posted on the class web site. These data come from a study of voluntary counseling and testing (VCT) for HIV at Mulago Hospital in Kampala, Uganda. */ use vct.dta, clear *Stata lets us look at a spreadsheet of the data: */ browse /* First see what variables are in the dataset. Check how many observations and variables the dataset includes. */ describe /* Now get brief summary statistics for all the variables in the dataset. The full command we'll use is summarize, but we can shorten that to save typing. */ sum /* Now do a little data management to make the data easier to use. First assign some variable labels */ label var age "Age" label var auditc "Audit score, last 3 months" label var cd4 "CD4 count" label var cd4_cat "CD4 count" label var hiv "HIV status" label var hungry "How often household members go hungry" label var lastalc "Last time used alcohol" label var female "female sex" label var n_adults "# adults in household" label var n_child "# people <18yo in household" label var n_support "# people supported financially" label var religion "Religion" /* Now add some missing value labels. A easy-to-remember convention is to use the variable name as the name of the value label. (Stata doesn't have any problem with this). Other easy-to-remember conventions will also work (e.g., add an underscore to the variable name).*/ label define yesno 0 "no" 1 "yes" label values female yesno label define sex 1 "men" 2 "women" label values sex sex label define hiv 0 "HIV-" 1 "HIV+" label values hiv hiv label define cd4_cat 0 "<50" 1 "51-250" 2 "251-500" 3 ">500" label values cd4_cat cd4_cat label define hungry 1 "often" 2 "sometimes" 3 "seldom" 4 "never" label values hungry hungry label define lastalc 0 "never" 1 ">1 year ago" 2 "within last year" label values lastalc lastalc label define religion 1 "Protestant" 2 "Catholic" 3 "Saved" 4 "Moslem" 8 "Other" label values religion religion /* You can use these commands to get information about the values labels for a variable:*/ codebook religion label list religion /* Note the use of the indicator female for sex, coded 0 = no and 1 = yes. Indicators coded like this have several advantages: * the mean value of the indicator gives the proportion of women in the dataset * this is the coding required for binary outcomes in logistic models * unlike a variable for sex, coded 1 = male 2 = female, the indicator does not require us to remember the coding and value labels Now take another look at what we have. The value labels should show up in the browser. */ des sum browse /* Note that CD4 count is present only for the 30% of participants who are HIV+. ******************************************************************************** Creating and modifying new variables To create a new variable, use the generate command; to modify it, use the replace command, and to drop a variable, use the drop command. In making variables, you will often need to use so-called logical operators: equal: == not equal: ~= or ~= and: & or: | greater: > or >= less: < or <= Missing numeric values are denoted . (for missing character strings use ""). Note that missing numeric values are stored as a very large number, (always the largest in the dataset). As a result, if you are missing age values for 2 persons in the data set, and you create a new variable that includes all participants with age>30, you will include the 2 with missing data. Example: AUDIT-C is a scale measuring alcohol consumption, ranging from 0-12. For those not currently drinking (on this survey it is the past 3 months) the score is 0, for those drinking the score is 1-12. To make a new variable that represents any alcohol consumption we can use this following code: */ gen alc_any=0 if auditc==0 replace alc_any=1 if auditc>0 & auditc !=. label var alc_any "Any alcohol use" label values alc_any yesno // value label yesno is already part of the dataset * check to make sure this worked tab auditc alc_any, missing /* The second condition ensures that missings are kept as missing. A better way to do this is to use the recode command, which will handle the missing values correctly: */ drop alc_any recode auditc (0=0 "no") (2/max=1 "yes"), gen(alc_any) label var alc_any "Any alcohol use" * check to make sure this worked tab auditc alc_any, missing /* We can also use recode and replace to make a more complex indicator for unhealthy alcohol consumption, using different cutoff for men (>=4) and women (>=3): */ recode auditc (min/2=0 "no") (3/max=1 "yes"), gen(alc_unh) replace alc_unh = 0 if female==0 & auditc==3 label var alc_unh "Unhealthy alcohol use" * check to make sure this worked bysort female: tab auditc alc_unh, m /* Note the abbreviated name for the unhealthy alcohol use indicator. Stata does allow long, explicit variable names, with some limits, but they can be tedious to use in programming; better to encode the extra information in the variable label. Note also that all the alcohol variables we have made start with the same stem alc_, also with a view towards making them easier to remember. Finally, we can use recode to categorize the continuous variable, age. In doing this, we can use the ordering of the elements of the command to control whether the values at each cutpoint are put into the lower or upper category. Specifically, values equal to a cutpoint get put into the class defined by the element where the cutpoint is first encountered. We will first code age categories <=20, >20 to <=30, >30 to <=40, >40 to <=50, and >50. */ qui recode age (min/20=1 "<=20") (20/30=2 ">20 to 30") (30/40=3 ">30 to 40") /// (40/50=4 ">40 to 50") (50/max=5 ">50"), gen(age_cat) label var age_cat "Age" * check this tabstat age, by(age_cat) s(min max) /* To include the cutpoints in the upper category, we can reverse the order of the elements of the command: */ drop age_cat qui recode age (50/max=5 "50+") (40/50=4 "40 to <50") (30/40=3 "30 to <40") /// (20/30=2 "20 to <30") (min/20=1 "<20") , gen(age_cat) label var age_cat "Age" * check this tabstat age, by(age_cat) s(min max) /* Put the variables in alphabetical order, compress anything stored wastefully, get a final look at the dataet, and save the modified dataset. */ compress aorder des sum /* Finally, save the revised dataset. It's good practice never to write over the input dataset. */ save vct_revised, replace /* Now close the log file.*/ log close