Hello,
I am attempting to lag a variable by region, month, and year. Essentially, I need to create a new variable that is the lagged mean value (by year and month) for a neighboring region.
I have this:
region month year mean
1 1 00 5
1 2 00 6
1 3 00 4
1 1 01 4
1 2 01 5
1 3 01 5
2 1 00 4
2 2 00 5
2 3 00 3
2 1 01 3
2 2 01 5
2 3 01 2
and I need:
region month year mean neighbor_mean_lag
1 1 00 5 .
1 2 00 6 .
1 3 00 4 .
1 1 01 4 10
1 2 01 5 13
1 3 01 5 16
2 1 00 10 .
2 2 00 13 .
2 3 00 16 .
2 1 01 3 .
2 2 01 5 .
2 3 01 2 .
(Note neighbor_mean_lag equals the neighboring region's monthly values for the previous year.)
I am able to create a lagged variable for the same region using the below code. However, I haven't been successful at creating the lagged variable for a neighboring region.
DATA lag3; SET lag2; BY region month year; lag1_mean=lag(mean); if first.month and first.year then do; lag1_mean=.; end; RUN;
Thank you for your help!
... View more