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

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

Compute total kilograms<br />

produced<br />

The generate command<br />

From the output you should see there are the same<br />

number of records in the file as there was be<strong>for</strong>e the<br />

merge, i.e. 1,693. Note that there are 27 cases where<br />

there was not a match <strong>for</strong> the prod-unit combination in<br />

the look-up file. How would you specify the “list”<br />

command to look at these 27 cases?<br />

You would want to investigate further to see if the<br />

records without a look-up value are crops that you want<br />

to have included in the analysis you are doing, and, if<br />

they are, correct the lookup file and/or the production file<br />

and run your procedure again.<br />

We can now calculate total kilograms by multiplying the<br />

number of units (p1b) by the conversion factor.<br />

1. Select Create or change variables from the<br />

Data menu<br />

2. Select Create new variable<br />

The Generate - Create a new variable dialog box<br />

opens.<br />

3. Under the Main tab, change the Variable type to<br />

“double”.<br />

4. Type the name of the new variable in the<br />

Variable name box: qprod_tt<br />

5. For the Contents of new variable box, type in<br />

p1b * conver<br />

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

7. Add a comment in the do-file to explain what<br />

you have done.<br />

The Stata command is:<br />

generate double qprod_tt= p1b * conver<br />

Note that there were 49 cases where no value was<br />

generated. We had 27 cases with no conversion value.<br />

Why do the rest of the cases (22 cases) not have values?<br />

Now that the kilograms have been calculated we need to<br />

look up the value of a kilogram in calories <strong>for</strong> each<br />

product. This in<strong>for</strong>mation is in the table lookup file<br />

called calories.dta. This file has two variables —<br />

product and number of calories per kilogram. The key<br />

variable is product (prod). In order to add the calorieconversion<br />

variable to the working data file we need to<br />

do another merge with keyed table lookup (joinby). This<br />

time the key variable only needs to be the product<br />

variable. The data file has already been sorted by product<br />

60

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

Saved successfully!

Ooh no, something went wrong!