I have data like this
year month X Y weight
2013 1 1 0 1000
2001 12 0 1 2000
I want to create a variable Z
based on the X
and Y
variables, conditional on year
. I have two formulas for year
before and after 2002. If I use egen
with if
,
if year > 2002 {
bysort year month :egen Z= total( x*weight)
}
else {
bysort year month : egen Z= total(y*weight*0.5)
}
this code is not going to work, because if year <2002
, Stata would report that z
has already been created. Is there any way to achieve the goal?
I used a very crude and brute force way to solve this problem. I create two variables for z, namely z and z_2002. Then I replace z with z_2002 if the year is less than 2002.