Economics should be open

November 14, 2008

STATA: Generating a bunch of lagged variables

Filed under: coding, Stata — Tags: , — howardchong @ 10:23 pm

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.

Create a free website or blog at WordPress.com.