How to efficiently create lag variable using Stata

有些话、适合烂在心里 提交于 2019-12-11 03:41:28

问题


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?


回答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.




回答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)



来源:https://stackoverflow.com/questions/31234458/how-to-efficiently-create-lag-variable-using-stata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!