Presentation is loading. Please wait.

Presentation is loading. Please wait.

{ Advanced Stata Programming Andrew Hicks CCPR Statistics and Methods Core.

Similar presentations


Presentation on theme: "{ Advanced Stata Programming Andrew Hicks CCPR Statistics and Methods Core."— Presentation transcript:

1 { Advanced Stata Programming Andrew Hicks CCPR Statistics and Methods Core

2 Automating your work 1.Macros 2.Saved Results 3.Loops 4.Egen commands 5.Writing programs 6.Ado files

3 Macros Assigns a string of text or a number to an abbreviation. ( a box you put text in) local name content local x 1 (italics indication something you provide) display `x’

4 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. Global macros persist until you delete it or exit Stata. local x 1 global x 1

5 Macros Assigns a string of text or a number to an abbreviation. regress price headroom trunk weight length local size “headroom trunk weight length” `size’ regress price `size’ regress mpg `size’ regress gear_ratio `size’ foreign turn ( a box you put text in)

6 Results saved by Stata Stata provides the number so you don’t have to enter by hand. summarize price return list generate price_centered2 = price – r(mean) generate price_centered = price –

7 Loops 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) Center 5 variables from Auto dataset: price, mpg, weight, length, turn, displacement Manually:

8 Foreach Loops foreach var in price mpg weight length turn displacement { summarize `var’ generate `var’_centered2 = `var’ – r(mean) } Center 6 variables from Auto dataset: price, mpg, weight, length, turn, displacement Loop (foreach): 1. summarize price generate price _centered2 = price – r(mean) 2. summarize mpg generate mpg _centered2 = mpg – r(mean) summarize displacement generate displacement _centered2 = displacement – r(mean)

9 Foreach Loops foreach depvar in weight length turn displacement { regress `depvar’ price mpg headroom trunk } Loop (foreach): foreach item in quest1 qeust2 qeust3 quest4 { replace `item’=. if `item’ == 99 } foreach item of varlist quest* { replace `item’=. if `item’ == 99 } foreach depvar of varlist weight-displacement { regress `depvar’ price mpg headroom trunk }

10 Foreach Loops 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 Create your own foreach loop: foreach lname in list { commands referring to lname }

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

12 Forvalues Loops forvalues year=1990/2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. } Loop (forvalues):

13 Forvalues Loops Loop (forvalues): forvalues year=1990/2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. } forvalues year=1990(2)2010 { gen hadInc`year’=(inc`year’>0) if inc`year’<. } foreach year of numlist { gen hadInc`year’=(inc`year’>0) if inc`year’<. } forvalues race=1/3 { svy, subpop(if race==`race’): regress income age i.education } levelsof race, local(races) foreach race in races { svy, subpop(if race==`race’): regress income age i.education }

14 Forvalues Loops 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 Create your own forval loop: forvalues lname = range { commands referring to lname }

15 Nested Loops forval i=1/3 { forval j=1/3 { display “`i’, `j’” }

16 Nested Loops forval year = 1990/2010 { foreach month 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’<. }

17 Loops with counter local counter = 0 foreach var in price mpg weight length turn displacement { local counter = `counter’ + 1 display “`counter’ - `var’” } local counter = `counter’ + 1 local ++counter

18 While Loops foreach and forvalues loops repeat a command a set number of times: forval i=1/5 { display `i’ } while loops repeat until a condition is no longer true: local i 1 while `i’<=5 { display `i++’ }

19 Egen commands 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: egen mean_score = rowmean(read write math science) egen mean_score = rowmean(score1 score2 score3 score4) egen mean_score = rowmean(score*) = rowtotal(score*) = rowmax(score*) = rowmin(score*)

20 Egen commands rowmean( ) averages across variables mean( ) averages across observations egen mean_read = mean(read) egen schooltype_mean_read = mean(read), by(schtyp)

21 Writing programs 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: capture program drop hello program define hello di “Hello `0’” end

22 Writing programs A program to center variables: program define demean foreach var of local 0 { quietly: sum `var' replace `var'=`var'-r(mean) } end

23 Writing programs A program to center variables: program define demean foreach var 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

24 Ado files How to make an ado file: 1.Write a program in a do-file 2.Save the do-file with.ado extension demean.ado 3.Put the.ado file in your personal ado directory sysdir PERSONAL: c:\ado\personal\ 4.Use your ado program just like any other Stata command


Download ppt "{ Advanced Stata Programming Andrew Hicks CCPR Statistics and Methods Core."

Similar presentations


Ads by Google