/*Last modified 1/15/2013 */ /*This program illustrates some commonly used Stata commands*/ ************************************************************************* *The following block of commands go at the start of nearly all do files*/ *Bracket comments with /* */ or just use an asterisk if the comment starts at the beginning of a line clear set mem 50m /*Adjust this for your particular dataset*/ cd "C:\DATA\Courses\Econ 562\homework" /*Change this for your file structure*/ log using stataexample.log, replace /*Log files records all commands and results*/ display "$S_DATE $S_TIME" set more off insheet using eitc.out, clear /*Reading in text data*/ use eitc.dta, clear /*Reading in a stata dataset*/ ************************************************************************* /*Documenting and Describing your data*/ des compress des label var state "State of Residence" label var year "Year" label data "Earned income tax credit dataset" des sum sum children, detail tab children nonwhite table nonwhite table nonwhite, contents(mean earn mean unearn p95 earn n age) table nonwhite, c(mean earn mean unearn p95 earn n age)row format(%9.2f) center /*If your command lines are long, change the delimter*/ #delimit ; table nonwhite children, c(mean earn mean unearn p95 earn n age)row format(%9.2f) center #delimit cr /*Alternative approach to long lines*/ table nonwhite children, /// c(mean earn mean unearn p95 earn n age)row format(%9.2f) center /*ADD A COMMAND HERE: read the help command for table and then make your own table*/ histogram children scatter earn unearn scatter children nonwhite /*ADD A COMMAND HERE: use the Graphics pull down tab and make your own graph*/ list age children state year urate in 1/25 sort state year list age children state year urate in 1/25 ************************************************************************* /*Creating and Manipulating Simple Variables*/ rename finc faminc gen log_earn = log(earn) label var log_earn "Log of earned income" /*Label any new vars you make*/ gen ba = 1 if ed==11 replace ba = 0 if ed<11 drop ba gen ba = (ed==11) /*Evaluate, 1 for true, 0 for false*/ label var ba "Dummy=1 if ed==11" egen meanearn = mean(earn) egen medearn = median(earn) /*ADD A COMMAND HERE: make a variable using gen and one using egen*/ sum log_earn ba meanearn medearn /*Sum your new vars to make sure didn't make an error*/ drop log_earn ba meanearn medearn drop ba capture drop ba /*Capture supresses the error message*/ ************************************************************************* /*Making dummy variables*/ gen child1 = 1 if children==1 /*Note difference between assignment (=) and equality (==)*/ replace child1 = 0 if children~=1 tab child1 drop child1 gen child1 = (children==1) tab child1 drop child1 tab children, gen(children) sum children* /* Note the asterisk*/ xi: reg earn i.children drop _I* children1-children10 /*Note the variable range*/ ************************************************************************* /*Using by, _n, _N */ tab nonwhite, sum(earn) by nonwhite, sort: sum(earn) by nonwhite, sort: egen groupmean =mean(earn) tab nonwhite groupmean gen small_n = _n /* _n is observation number and _N is number of final ob*/ gen earn3 = earn[3] gen big_n = _N list earn small_n earn3 big_n in 1/25 by nonwhite, sort: gen avgearn = sum(earn) /*This is a silly example to show you how _n and _N work*/ list earn avgearn in 1/25 drop avgearn by nonwhite, sort: gen avgearn = sum(earn)/_N list earn avgearn in 1/25 by nonwhite: replace avgearn = avgearn[_N] tab avgearn drop small_n earn4 big_n avgearn /*ADD A COMMAND HERE: use the by prefix with some other command*/ ************************************************************************* /*Regression analysis*/ reg earn children nonwhite test children test children nonwhite test children=nonwhite test children=nonwhite=1 search breusch pagan predict earnhat predict error, resid list earn earnhat error in 1/25 drop earnhat error capture drop earnhat outreg2 using junkresults, word see /*You will need to install outreg2*/ outreg2 using junkresults, dec(2) noparen word replace see reg earn children nonwhite, robust seeout outreg2 using junkresults, dec(2) noparen word append see /*ADD A COMMAND HERE: run another regression and append the results*/ ************************************************************************* /*Working with panel data*/ by year, sort: reg earn children, robust sort state year collapse urate children earn, by(state year) /*Only keeps the variables in the list and the panel variables you specify*/ list state year urate children earn in 1/25 label data "State and year level EITC data" reshape wide children earn urate , i(state) j(year) list in 1/25 reshape long children earn urate, i(state) j(year) list in 1/25 exit, clear