Cross-posted on stack overflow.


I need help in generating a lead variable in a panel. I want to generate a lead income variable (let's call it V1) by CMA, Province and Year such that the data looks like;
CMA Province Year Income V1 TO A 1990 $5 $8 TO A 1991 $8 . MT A 1990 $12 $7 MT A 1991 $7 . AB B 1990 $5 $15 AB B 1990 $15 . etc
I was using the following codes, but I was getting only missing values.
gsort CMA Province -Year Income gen ld_inc = Income[_n-1] if Year == Year[_n-1] + 1 replace ld_inc = ld_inc[_n-1] if Year == Year[_n-1] & missing(ld_inc)
Is there something wrong with my code and what I am getting wrong? Thanks in advance.