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 />
The <strong>for</strong> z in num 1/3 looping<br />
command<br />
The <strong>for</strong>each looping command<br />
The levelsof command<br />
2. The dialog box opens. In the New Variable box: type<br />
quart.<br />
3. In the Expression: box type cprod_ae.<br />
4. In the “Options” section, select 4 quintiles.<br />
5. Click on the “If/In tab and in the if: expression type<br />
district == 1.<br />
This command would have to be repeated <strong>for</strong> the other<br />
two districts so that 3 variables are created where the<br />
observations <strong>for</strong> that district are divided equally into 4<br />
groups.<br />
Using the “if” expression works where you have only a<br />
few codes within the variable. We have 3 districts so it<br />
would not be a problem to use the if expression. What if<br />
we had 20 districts? This method would be a bit<br />
cumbersome.<br />
Another method is to use a counter. Add the command<br />
“<strong>for</strong> z in num 1/3” in front of the xtile command, where z<br />
is a temporary variable that loops through the values<br />
specified with the “num 1/3”. The value of 3 would be<br />
replaced with the number of values in the district<br />
variable. Note that the “z” is added to the “quart”<br />
variable and that “z” is used instead of the actual numeric<br />
value <strong>for</strong> the value to use <strong>for</strong> the value <strong>for</strong> district.<br />
<strong>for</strong> z in num 1/3: xtile quartz = cprod_ae if<br />
district==z, nq(4)<br />
Stata provides another looping command that we can use<br />
to compute the new ranking variable. It is not available<br />
through the menus. The looping command can be found<br />
in the Programming manual and is called <strong>for</strong>each. Stata<br />
added a new command called levelsof . The values are<br />
stored in temporary variables called r(levelsof). That<br />
in<strong>for</strong>mation can be stored in a local variable and the<br />
variable used to cycle through the values.<br />
1. Type the following command in the Command<br />
window:<br />
The results should display the values of the<br />
districts, e.g. 1 2 3<br />
2. Now let’s store that in<strong>for</strong>mation in a local<br />
variable. To make a temporary local level, we<br />
include the word “local” which means the variable<br />
only exists with the do-file. We need this<br />
76<br />
levelsof district