Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
ecode [R] recode<br />
Changes a variable's values, e.g. for grouping a continuous variable in<strong>to</strong> few groups. The<br />
inverted sequence ensures that age 55.00 (at the birthday) goes <strong>to</strong> category 4:<br />
recode age (55/max=4)(35/55=3)(15/35=2)(min/15=1) , generate(agegr)<br />
Value labels for the new variable may be included at once:<br />
recode age (55/max=4 "55+")(35/55=3 "35-54")(15/35=2 "15-34") ///<br />
(min/15=1 "-14") , generate(agegr)<br />
Very important: The generate option creates a new variable with the recoded<br />
information; without generate the original information in age will be destroyed.<br />
Other examples:<br />
recode expos (2=0) Leave other values unchanged<br />
recode expos (2=0) , gen(exp2) Values not recoded transferred unchanged<br />
recode expos (2=0) if sex==1 Values not recoded (sex != 1) set <strong>to</strong> missing<br />
recode expos (2=0) if sex==1 , copy Values not recoded (sex != 1) unchanged<br />
recode expos (2=0)(1=1)(else=.) Recode remaining values <strong>to</strong> missing (.)<br />
recode expos (missing=9) Recode any missing (., .a, .b etc.) <strong>to</strong> 9<br />
Another way <strong>to</strong> recode continuous data in<strong>to</strong> groups: [R] egen<br />
egen agegrp=cut(age) , at (0 5(10)85 120)<br />
age agegrp<br />
0 ≤ age < 5 0<br />
5 ≤ age < 15 5<br />
15 ≤ age < 25 15<br />
.. ..<br />
85 ≤ age < 120 85<br />
for<br />
Enables you with few command lines <strong>to</strong> repeat a command. To do the modulus 11 test for<br />
Danish CPR numbers (see section 15.3) first multiply the digits by 4,3,2,7,6,5,4,3,2,1; next<br />
sum these products; finally check whether the sum can be divided by 11. The CPR numbers<br />
were split in<strong>to</strong> 10 one-digit numbers c1-c10:<br />
generate test = 0<br />
for C in varlist c1-c10 \ X in numlist 4/2 7/1 : ///<br />
replace test = test + C*X<br />
replace test = mod(test,11) // Remainder after division by 11<br />
list id cpr test if test !=0<br />
C and X are stand-in variables (names <strong>to</strong> be chosen by yourself; note the use of capital<br />
letters <strong>to</strong> distinguish from existing variables), <strong>to</strong> be sequentially substituted by the elements in<br />
the corresponding list. Each list must be declared by type; there are four types: newlist<br />
(list of new variables), varlist list of existing variables, numlist (list of numbers),<br />
anylist (list of words).<br />
for is not documented in the manuals any more. The foreach and forvalues<br />
commands partially replace it, but they don't handle parallel lists as shown above. See section<br />
15.7.<br />
22