Friday, April 28, 2006

Create lag varaible in Stata

Q. How to create a lag variable in Stata?

# if dataset is not of time series, which is wierd, but anyway
gen varlag=var[_n-1]

# if dataset is of time series or cross-sectional or panel ("L." is the operator)
tsset timevar, panelid
gen varlag=l.var

or

by panelid: gen varlag=var[_n-1]

4 comments:

Anonymous said...

panelvar and datevar are the wrong way around

Yu-Sung Su said...

Thanks for your comment.

Actually it doesn't matter. Stata will know the difference.

Anonymous said...

necessita di verificare:)

Pailtsay said...

I saw your posting, and am wondering if you can help me with lag variable in STATA 11SE.

I am trying to run a regression for sales=c+beta(newsreporting) + salesL where salesl suppose to be infinitely lagged. but I am having trouble creating the lagged sales variable for some reason