Question about lagged variables in stata
Since the lagged variables won't autometically change to reflect the change in current varible, I'm wondering how to make it adaptive to the change of current variable, which can be a formula, instead of mannually updating it every time.
For example, HAt= 0.1*roa1+0.9*HAt-1,
I have to calculate HA (HistoricA).
by gvkey year, sort: gen HistoricA =.
by gvkey year, sort: gen HistoricA1 = HistoricA[_n-1]
by gvkey: replace HistoricA1 = roa if _n == 1
by gvkey: replace HistoricA = 0.1 * roa1 + 0.9 * HistoricA1 if _n > 1
The above coding doesn't work. It gives me the result like :
gvkey year roa roa1 roa2 HistoricA HistoricA1
1004 1999 .0474536 . .0474536
1004 2000 .0264029 .0474536 . .
1004 2001 -.0829894 .0264029 . 0474536 .
1004 2002 -.018074 -.0829894 . 0264029 .
1004 2003 .0049401 -.018074 -. 0829894 .
Is there any loop function or other codes could solve the automatically change problem?
Appreciate your help in advance!
Since the lagged variables won't autometically change to reflect the change in current varible, I'm wondering how to make it adaptive to the change of current variable, which can be a formula, instead of mannually updating it every time.
For example, HAt= 0.1*roa1+0.9*HAt-1,
I have to calculate HA (HistoricA).
by gvkey year, sort: gen HistoricA =.
by gvkey year, sort: gen HistoricA1 = HistoricA[_n-1]
by gvkey: replace HistoricA1 = roa if _n == 1
by gvkey: replace HistoricA = 0.1 * roa1 + 0.9 * HistoricA1 if _n > 1
The above coding doesn't work. It gives me the result like :
gvkey year roa roa1 roa2 HistoricA HistoricA1
1004 1999 .0474536 . .0474536
1004 2000 .0264029 .0474536 . .
1004 2001 -.0829894 .0264029 . 0474536 .
1004 2002 -.018074 -.0829894 . 0264029 .
1004 2003 .0049401 -.018074 -. 0829894 .
Is there any loop function or other codes could solve the automatically change problem?
Appreciate your help in advance!
Comment