I really need help on this: I want to use a lagged variable in my
analysis but instead of lagging based on some pre-existing value, this
lag actually comes from certain calculation. My data is a panel data
(grouped by "firmid") with a structure like:
firmid year age_a age_b weight_a weight_b
100 1960 10
100 1961 11 2 .9 .1
100 1962 12
100 1963 13 10 .7 .3
100 1964 14
100 1965 15
100 1966 16 5 .8 .2
101 1940 1
101 1941 2 10 .6 .4
101 1942 3
The goal is to calculate an adjusted age for each firm-year. In the
data, age_a is the natural age of the firm, while age_b comes from
certain event (say, an acquisition). Before there is any event, the
firm's adjusted age is simply the natural age (age_a). When there is
an event, the new adjusted age is the weighted sum of the adjusted age
and age_b (weight_a is the weight for adjusted age while weight_b for
age_b). When there is no event, the firm's adjusted age simply
increase by 1 for each year. I thus wrote the following program:
/** Start of the program ***/
clear
input firmid year age_a age_b weight_a weight_b
100 1960 10 . . .
100 1961 11 2 0.9 0.1
100 1962 12 . . .
100 1963 13 10 0.7 0.3
100 1964 14 . . .
100 1965 15 . . .
100 1966 16 5 0.8 0.2
101 1940 1 . . .
101 1941 2 10 0.6 0.4
101 1942 3 . . .
end
sort firmid year
gen age=0
by firmid: replace age=age_a if _n==1 & age_b==.
by firmid: replace age=(age_a*weight_a + age_b*weight_b) if _n==1 & age_b~=.
by firmid: replace age=(age[_n-1]+1)*weight_a + age_b*weight_b if
_n~=1 & age_b~=.
by firmid: replace age=(age[_n-1]+1) if _n~=1 & age_b==.
/** End of the program **/
The result, however, came quite strange:
firmid year age_a age_b weight_a weight_b age
100 1960 10
10
100 1961 11 2 .9 .1
10.1
100 1962 12
11.1
100 1963 13 10 .7 .3 3.7
100 1964 14
4.7
100 1965 15
5.7
100 1966 16 5 .8 .2
1.8
101 1940 1
1
101 1941 2 10 .6 .4
5.2
101 1942 3
6.2
For the 4th and 7th observations (where "year" are 1963 and 1966
respectively), the correct ages should be about 11
([11.1+1]*0.7+10*0.3) and 6 ([5.7+1]*0.8+5*0.2). Obviously, instead of
getting the actual last "age" values, STATA took the very original
ones - the generated 0s. I could not figure it out and really
appreciate any help on this.
Thanks a lot!
Robin Luo
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/