STATA 11 for Windows SAMPLE SESSION - Food Security Group ...
STATA 11 for Windows SAMPLE SESSION - Food Security Group ...
STATA 11 for Windows SAMPLE SESSION - Food Security Group ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Stata <strong>11</strong> Sample Session Section 2 – Restructuring Data Files – Table Lookup & Aggregation<br />
check <strong>for</strong> number of districts */<br />
tabulate district<br />
/* there are 3 districts so we want to loop 3 times */<br />
*first method.<br />
<strong>for</strong> z in num 1/3: xtile quartz=cprod_ae if district == z, nq(4)<br />
/*initialize variable */<br />
gen quart=.<br />
/*replace values with in<strong>for</strong>mation from temporary variable */<br />
<strong>for</strong> z in num 1/3: replace quart=quartz if district==z<br />
<strong>for</strong> z in num 1/3: drop quartz<br />
/* check results - should see equal number of cases in each category */<br />
tabulate quart district<br />
*second method.<br />
drop quart.<br />
*second method to create the quartile variables<br />
*the following stores those numbers into local variables<br />
levelsof district, local(levels)<br />
<strong>for</strong>each z of local levels {<br />
xtile quartile`z' = cprod_ae if (district==`z'), nquantiles(4)<br />
}<br />
*create a new variable<br />
generate quart = .<br />
*replace values from quartile1, quartile2, quartile3 into quart<br />
levelsof district, local(levels)<br />
<strong>for</strong>each z of local levels {<br />
replace quart= quartile`z' if district == `z'<br />
}<br />
/*delete the 3 extra variables */<br />
levelsof district, local(levels)<br />
<strong>for</strong>each z of local levels {<br />
drop quartile`z'<br />
}<br />
tabulate quart district<br />
label variable quart "Calorie production quartile"<br />
/* produce the table */<br />
by district quart sort: summarize cprod_ae<br />
/* sort file by key variables */<br />
sort district vil hh<br />
save "hh-file3.dta", replace<br />
*close log file<br />
log close<br />
85