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

/* open production data file */<br />

use "c-q4.dta", clear<br />

/* sort variables to match by<br />

to merge in the conversion value to convert to kgs */<br />

sort prod p1a<br />

tab1 prod p1a<br />

/* rename the p1a variable to unit to match the conver data file */<br />

rename p1a unit<br />

joinby prod unit using "conver.dta", unmatched( master ) _merge(_merge)<br />

*check to be sure merge done correctly<br />

tab1 _merge<br />

/* check to see if got what was expected using list command */<br />

list prod unit conver if prod==47 & unit ==8<br />

* calculate kgs produced<br />

generate double qprod_tt= p1b * conver<br />

/* merge in the lookup conversion value <strong>for</strong> calories and calculate total calories */<br />

drop _merge<br />

joinby prod using "calories.dta", unmatched( master ) _merge(_merge)<br />

*check to be sure merge done correctly<br />

tab1 _merge<br />

*compute total calories produced<br />

generate double cprod_tt= qprod_tt * calories<br />

/* add variable labels */<br />

label variable qprod_tt "Total production in kgs"<br />

label variable cprod_tt "Total calories produced"<br />

/* select only staple crops */<br />

keep if prod == 5 | prod == 6 | prod == 30 | prod == 31 | prod == 41 | prod ==44 | prod == 47<br />

/* check to see that there are only 7 crops listed */<br />

tabulate prod<br />

/* need to sum all calories produced by the household<br />

Using the collapse command*/<br />

collapse (sum) cprod_tt, by(district vil hh)<br />

label variable cprod_tt "Calories produced in staple foods"<br />

describe<br />

/* verify you have the right average calories produced over whole sample */<br />

summarize cprod_tt<br />

/* save the file */<br />

83

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

Saved successfully!

Ooh no, something went wrong!