Hello, I have a question regarding my code. I would like to include a time lag to account for the fact that changes in var2 have a delayed impact on var1. Therefore, I want to regress the observations of var2 on var1 for the following year for each ID. I attempted to do this with the following code, but afterwards, var2 takes on the same values for each year.
sort id year
list year id var1 var2 if id==1
replace var1 = var2[_n-1] if id==id[_n-1]
list year id var1 var2 if id==1
However, if I do it the other way around and reset var2 by one year, it works.
sort id year
list year id var1 var2 if id==1
replace var1 = var2[_n+1] if id==id[_n+1]
list year id var1 var2 if id==1
Does anyone know where the error in my code is? Thank you very much!
sort id year
list year id var1 var2 if id==1
replace var1 = var2[_n-1] if id==id[_n-1]
list year id var1 var2 if id==1
However, if I do it the other way around and reset var2 by one year, it works.
sort id year
list year id var1 var2 if id==1
replace var1 = var2[_n+1] if id==id[_n+1]
list year id var1 var2 if id==1
Does anyone know where the error in my code is? Thank you very much!
Comment