How to efficiently create lag variable using Stata

19,524

Solution 1

@Robert has shown you the streamlined way of doing it. For completion, here is the "traditional", boring way:

clear
set more off

*----- example data -----

set obs 2

gen id = _n
expand 20

bysort id: gen time = _n
tsset id time

set seed 12345
gen x = runiform()
gen y = 10 * runiform()

list, sepby(id)

*----- what you want -----

// "traditional" loop
forvalues i = 1/10 {
     gen x_`i' = L`i'.x
     gen y_`i' = L`i'.y
}

list, sepby(id)

And a combination:

// a combination
foreach v in x y {
    tsrevar L(1/10).`v'
    rename (`r(varlist)') `v'_#, addnumber
}

If the purpose is to create lagged variables to use them in some estimation, know you can use time-series operators within many estimation commands, directly; that is, no need to create the lagged variables in the first place. See help tsvarlist.

Solution 2

You can loop to do this but you can also take advantage of tsrevar to generate temporary lagged variables. If you need permanent variables, you can use rename group to rename them.

clear
set obs 2
gen id = _n
expand 20
bysort id: gen time = _n
tsset id time
set seed 12345
gen x = runiform()
gen y = 10 * runiform()
tsrevar L(1/10).x
rename (`r(varlist)') x_#, addnumber
tsrevar L(1/10).y
rename (`r(varlist)') y_#, addnumber

Note that if you are doing this to calculate a statistic on a rolling window, check out tsegen (from SSC)

Share:
19,524
fly36
Author by

fly36

Updated on June 14, 2022

Comments

  • fly36
    fly36 almost 2 years

    I have panel data (time: date, name: ticker). I want to create 10 lags for variables x and y. Now I create each lag variable one by one using the following code:

    by ticker: gen lag1 = x[_n-1]
    

    However, this looks messy.

    Can anyone tell me how can I create lag variables more efficiently, please?

    Shall I use a loop or does Stata have a more efficient way of handling this kind of problem?