From the online help for the function:
The queue for each occurrence of LAGn is initialized with n missing values, where n is the length of the queue (for example, a LAG2 queue is initialized with two missing values). When an occurrence of LAGn is executed, the value at the top of its queue is removed and returned, the remaining values are shifted upward, and the new value of the argument is placed at the bottom of the queue. Hence, missing values are returned for the first n executions of each occurrence of LAGn, after which the lagged values of the argument begin to appear.
The key part is the "EACH OCCURENCE". When you have an If Thisvar>5 then Value=Lag3(othervar); the Occurence is not every record only the record(s) where ThisVar is > 5.
Please see
data example;
do outer=1 to 5;
do inner= 3 to 7;
output;
end;
end;
run;
data result;
set example;
if inner> 5 then do;
newval=lag3(outer);
other = lag2(inner);
end;
run;
Tracing down why "other" = inner and not the value two before will be educational. It has to do that the stack is only updated when Inner is > 5, so 6 and 7 are the only values the lag2(inner) sees.
And the first non-missing value of newval = 1 because the lag stack is 3 deep and the first time the stack has a non-missing value for Lag3 is looking at the outer=1 and inner=6.
You might expect to get a value for inner=4,5,6,7
... View more