CJ12
CJ12

Reputation: 495

Formula that uses previous value

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

Upvotes: 1

Views: 636

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

Your code can be made a little more concise. Here's how:

input X   count   ID
.     1    1
2     2    1
1     3    1
3     4    1
2     5    1
.     1    2
1     2    2
7     3    2
end 
gen Y = count == 1 
bysort ID (count) : replace Y = (1 + X) * Y[_n-1] if count > 1 

The creation of a dummy (indicator) variable can exploit the fact that true or false expressions are evaluated as 1 or 0.

Sorting before by and the subsequent by command can be condensed into one. Note that I spelled out that within blocks of ID, count should remain sorted.

This is really a comment, not another answer, but it would be less clear if presented as such.

Upvotes: 1

CJ12
CJ12

Reputation: 495

Y-1, the lag in the formula would be translated as seen in the below.

gen Y = 0 
replace Y = 1 if count == 1
sort ID
by ID: replace Y = (1+X)*Y[_n-1] if count != 1

Upvotes: 0

Related Questions