This small blog post is just a note on how to create a bunch of lagged variables using a simple forvalues loop.
de
* this gives you a list of your variables
foreach varname in varlist qqq - zzz {
* this says to generate lagged variables for all variables in the
* variable list between qqq and zzz
forvalues i=1/9 {
*generate 9 lagged values for each
by date, sort: gen lag`i'`varname'=`varname'[_n-`i']
}
}
so, if you have variables 10 varaibles between qqq and zzz inclusive, this script will generate 9 lagged variables for each.
I just use l.
(assuming I’ve used tsset early to enable all the fun panel data features)
Comment by llamartin — January 8, 2009 @ 3:40 pm
oops the brackets were treated as tags: l.[varname]
Comment by llamartin — January 8, 2009 @ 3:41 pm
Yes, l. is a very cool thing.
Comment by howardchong — April 7, 2009 @ 9:10 pm