In Stata I want to have a variable calculated by a formula, which includes multiplying by the previous value, within blocks defined by a variable ID
. I tried using a lag but that did not work for me.
In the formula below the Y-1
is intended to signify the value above (the lag).
gen Y = 0
replace Y = 1 if count == 1
sort ID
by ID: replace Y = (1+X)*Y-1 if count != 1
X Y count ID
. 1 1 1
2 3 2 1
1 6 3 1
3 24 4 1
2 72 5 1
. 1 1 2
1 2 2 2
7 16 3 2