Hello, I am trying to understand how the LAGn function works. I have the following test code (it doesn't really make sense, just did it to understand the processing): data demo;
input ID $ day;
cards;
A 1
A 2
A 3
A 4
A 5
A 6
A 7
A 8
A 9
;
data test;
set demo;
three_days_ago=lag3(day);
if mod(day, 2)=0 then three_days_ago_even=lag3(day);
run;
According to my current understanding LAGn creates a FIFO queue of size n and each time it's called it returns the first value in the queue (the oldest added) and adds the value of the lagged variable at the current observation to the end of the queue. So I supposed I would get something like this: ID day three_days_ago three_days_ago_even (initial queue: {., ., .}) A 1 . . (queue returns ., queue after step: {., ., 1} A 2 . . (queue returns . and ., queue after step: {1, 2, 2} A 3 1 . (queue returns 1, queue after step: {2, 2, 3} A 4 2 2 (queue returns 2 and 2, queue after step: {3, 4, 4} A 5 3 . (queue returns 3, queue after step: {4, 4, 5} A 6 4 4 (queue returns 4 and 4, queue after step: {5, 6, 6} A 7 5 . (queue returns 5, queue after step: {6, 6, 7} A 8 6 6 (queue returns 6 and 6, queue after step: {7, 8, 8} A 9 7 . (queue returns 7, queue after step: {8, 8, 9} However, I got this: ID day three_days_ago three_days_ago_even A 1 . . A 2 . . A 3 . . A 4 1 . A 5 2 . A 6 3 . A 7 4 . A 8 5 2 A 9 6 . Could you please point out where I'm mistaken? Thank you in advance!
... View more