All 1 entries tagged Stata

No other Warwick Blogs use the tag Stata on entries | View entries tagged Stata at Technorati | There are no images tagged Stata on this blog

February 08, 2013

Stata syntax surprise

Here are two bits of syntax that have truly facilitated my life.

When faced with a load of survey variables to clean or recode or regress, the long-winded way is to do them all individually.


regress var1 iv1 iv2 iv3

regress var2 iv1 iv2 iv3

regress var3 iv1 iv2 iv3

Needless to say, it stops being fun after about var5.

So the solution is to set up a little macro. Say there are 7 variables we want to regress using the same regression model.

forval x=1/7 {

regress var`x' iv1 iv2 iv3


It's so beautiful!

But wait! What if your variable doesn't have a number? What if it's vara varb varc vard etc?

There is another beautiful loop.

foreach x of new `c(alpha)'{

dis "x'"

regress var`x' iv1 iv2 iv3


This loop will cycle through the aphabet. If you have variables from a to k, it will stop at k.

And these two little loops have revolutionised my syntax.

They can be used with anything!

My next mission: to find out how they can be combined.


Internet credits for the alpha loop:

Other useful stata sites and things: (love the syntax walkthroughs) (love the annotated outputs)

Publication-ready tables: try tabout outreg/outreg2 and estout

September 2023

Mo Tu We Th Fr Sa Su
Aug |  Today  |
            1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30   

Search this blog


Blog archive

RSS2.0 Atom
Not signed in
Sign in

Powered by BlogBuilder