Robin,
Stata is doing exactly what you typed - though what you want.
The third -replace- statement is true in 1963, though age in 1962 is still
0. 1962's age does not change until the fourth -replace- statement.
If you put -list- after each of your replace commands, you can see how Stata
is working through the commands.
That said, I believe this will accomplish what you want:
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
l
sort firmid year
qui {
gen age=0
by firm: replace age=age_a if _n==1 & age_b==.
by firm: replace age=(age_a*weight_a + age_b*weight_b) if _n==1 & age_b~=.
levels year, local(levels)
foreach l of local levels {
by firm: replace age = age[_n-1] + 1 if _n > 1 & age_b == . & year
== `l'
by firm: replace age = (age[_n-1]+1)*weight_a + age_b*weight_b if _n
>1 & age_b !=. & year == `l'
}
}
l
Hope this helps,
Scott
PS: It is bad form to post a message twice in the same day.
*
* 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/