250 likes | 430 Views
Advanced Stata Programming. Andrew Hicks CCPR Statistics and Methods Core. Macros Saved Results Loops Egen commands Writing programs Ado files. Automating your work. Assigns a string of text or a number to an abbreviation. ( a box you put text in). local name content.
E N D
Advanced Stata Programming Andrew Hicks CCPR Statistics and Methods Core
Macros Saved Results Loops Egen commands Writing programs Ado files Automating your work
Assigns a string of text or a number to an abbreviation. ( a box you put text in) local name content (italics indication something you provide) local x 1 display `x’ Macros
Assigns a string of text or a number to an abbreviation. local nobs = 198 display “The number of observations equals: `nobs’” display `nobs’ – 90 • Local macros can be used only within the do-file in which • the are defined. • When the program ends, the macro disappear. local x 1 Macros Global macros persist until you delete it or exit Stata. global x 1
Assigns a string of text or a number to an abbreviation. ( a box you put text in) regress price headroom trunk weight length `size’ local size “headroom trunk weight length” regress price `size’ Macros regress mpg `size’ regress gear_ratio `size’ foreign turn
Stata provides the number so you don’t have to enter by hand. summarize price return list Results saved by Stata generate price_centered = price – 6165.257 generate price_centered2 = price – r(mean)
Center 5 variables from Auto dataset: price, mpg, weight, length, turn, displacement Manually: summarize price generate price_centered = price – r(mean) summarize mpg generate mpg_centered = mpg – r(mean) summarize weight generate weight_centered = weight – r(mean) summarize length generate length_centered = weight – r(mean) summarize turn generate turn_centered = turn – r(mean) summarize displacement generate displacement = displacement – r(mean) Loops
Center 6 variables from Auto dataset: price, mpg, weight, length, turn, displacement Loop (foreach): foreachvar in price mpg weight length turn displacement { summarize `var’ generate `var’_centered2 = `var’ – r(mean) } 1. summarize price generate price_centered2 = price – r(mean) 2. summarize mpg generate mpg_centered2 = mpg – r(mean) Foreach Loops . . 6. summarize displacement generate displacement_centered2 = displacement – r(mean)
Loop (foreach): foreachdepvar in weight length turn displacement { regress `depvar’ price mpg headroom trunk } foreachdepvarof varlistweight-displacement { regress `depvar’ price mpg headroom trunk } foreachitem in quest1 qeust2 qeust3 quest4 { replace `item’=. if `item’ == 99 } Foreach Loops foreachitemof varlistquest* { replace `item’=. if `item’ == 99 }
Create your own foreach loop: generate taxinc1 = inc1 * .10 generate taxinc2 = inc2 * .10 generate taxinc3 = inc3 * .10 generate taxinc4 = inc4 * .10 generate taxinc5 = inc5 * .10 generate taxinc6 = inc6 * .10 generate taxinc7 = inc7 * .10 generate taxinc8 = inc8 * .10 generate taxinc9 = inc9 * .10 generate taxinc10 = inc10 * .10 generate taxinc11 = inc11 * .10 generate taxinc12 = inc12 * .10 foreachlname in list { commands referring to lname } Foreach Loops
Forvalues Loops gen hadInc1990 = (inc1990>0) if inc1990<. gen hadInc1991 = (inc1991>0) if inc1991<. . . gen hadInc2009 = (inc2009>0) if inc2009<. gen hadInc2010 = (inc2010>0) if inc2010<.
Forvalues Loops Loop (forvalues): forvaluesyear=1990/2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. }
Loop (forvalues): forvaluesyear=1990/2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. } forvaluesyear=1990(2)2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. } foreachyear of numlist1980 1983 1990{ gen hadInc`year’=(inc`year’>0) if inc`year’<. } forvaluesrace=1/3 { svy, subpop(if race==`race’): regress income age i.education } Forvalues Loops levelsofrace, local(races) foreachrace in races { svy, subpop(if race==`race’): regress income age i.education }
Create your own forval loop: generate taxinc1 = inc1 * .10 generate taxinc2 = inc2 * .10 generate taxinc3 = inc3 * .10 generate taxinc4 = inc4 * .10 generate taxinc5 = inc5 * .10 generate taxinc6 = inc6 * .10 generate taxinc7 = inc7 * .10 generate taxinc8 = inc8 * .10 generate taxinc9 = inc9 * .10 generate taxinc10 = inc10 * .10 generate taxinc11 = inc11 * .10 generate taxinc12 = inc12 * .10 forvalueslname=range { commands referring to lname } Forvalues Loops
forvali=1/3 { forval j=1/3 { display “`i’, `j’” } } Nested Loops
forvalyear = 1990/2010 { foreachmonth in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec { gen hadInc`month’`year’ = (inc`month’`year’>0) if inc`month’`year’<. } } Nested Loops
local counter = 0 foreachvar in price mpg weight length turn displacement { local counter = `counter’ + 1 display “`counter’ - `var’” } local ++counter Loops with counter local counter = `counter’ + 1 local ++counter
foreach and forvalues loops repeat a command a set number of times: forvali=1/5 { display `i’ } while loops repeat until a condition is no longer true: local i 1 while `i’<=5 { display `i++’ } While Loops
Take the average: generate mean_score = (read + write + math + science)/4 But if any single score is missing, the total score will be missing Solution: egenmean_score = rowmean(read write math science) egenmean_score = rowmean(score1 score2 score3 score4) Egen commands egenmean_score = rowmean(score*) = rowtotal(score*) = rowmax(score*) = rowmin(score*)
rowmean( ) averages across variables mean( ) averages across observations Egen commands egenmean_read = mean(read) egenschooltype_mean_read = mean(read), by(schtyp)
A simple program: program define hello di “Hello World” end A more complicated program: capture program drop hello program define hello di “Hello `1’” end An even more complicated program: Writing programs capture program drop hello program define hello di “Hello `0’” end
A program to center variables: program define demean foreachvar of local 0 { quietly: sum `var' replace `var'=`var'-r(mean) } end Writing programs
A program to center variables: program define demean foreachvar of local 0 { capture confirm numeric variable `var' if _rc==0 { sum `var',meanonly replace `var'=`var'-r(mean) } else di "`var' is not a numeric variable and cannot be demeaned." } end Writing programs
How to make an ado file: • Write a program in a do-file • Save the do-file with .ado extension • demean.ado • Put the .ado file in your personal ado directory • sysdir • PERSONAL: c:\ado\personal\ • Use your ado program just like any other Stata command Ado files