10.04.2013 Views

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 ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Stata <strong>11</strong> Sample Session Section 2 – Restructuring Data Files – Table Lookup & Aggregation<br />

Create a new file which is a<br />

household level file rather than a<br />

household-product level file<br />

The collapse command<br />

The “|” is a symbol <strong>for</strong> the word OR. We are<br />

telling Stata to select all cases with prod<br />

equal to 47 or prod equal 30 or prod equal 31<br />

and so on ...<br />

4. Click on the copy button, switch to the do-file<br />

editor, paste the command, switch back to the<br />

dialog box and click on Ok<br />

5. Add a comment to explain what you have done.<br />

The Stata command is:<br />

keep if prod==5 | prod==6 | prod==30 |<br />

prod==31 | prod==41 | prod==44 | prod==47<br />

Only cases with these product codes will now be used <strong>for</strong><br />

analysis. Note that 464 observations were dropped. You<br />

can use the tabulate command to verify that you now<br />

have only 7 crops in the file. In the Command window<br />

you can easily type<br />

tab prod<br />

There should be 1,239 cases remaining <strong>for</strong> the 7 crops<br />

that are considered staple foods.<br />

Now, we need to know how many calories were produced<br />

per household <strong>for</strong> all these 7 staple food products<br />

combined. To do this, we need to sum, <strong>for</strong> each<br />

household, the values of cprod_tt <strong>for</strong> all of the food<br />

crops the household produced. In other words, we need<br />

to create a new household-level file from the current<br />

household-product level file where there is only one case<br />

per household. Stata uses the command “collapse” to<br />

aggregate the number of cases at one level to a new level.<br />

We will sum all the cases <strong>for</strong> each household to create<br />

just one case <strong>for</strong> household.<br />

To create the new household-level file, we use the<br />

command: collapse. Stata always uses the working data<br />

file as the file to be collapsed.<br />

1. From the Data menu select Create or change<br />

variables then select Other variable<br />

trans<strong>for</strong>mation commands then select Make<br />

dataset of means, medians, etc.<br />

The Collapse - Make dataset of summary statistics<br />

dialog box will appear.<br />

2. On the Main tab in the Statistics: box <strong>for</strong> 1:<br />

change “mean” to “sum” by clicking on the dropdown<br />

arrow. In the Variables: box select<br />

cprod_tt .<br />

64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!