Because the LAG function stores values on the queue only when it is called, [b:bc182]you must call LAG unconditionally to get the correct answers[/b:bc182].
Example: Storing Every Other Lagged Value This example shows the difference in output when you use conditional and unconditional logic in your program.
options pagesize=25 linesize=64 nodate pageno=1;
title 'Store Every Other Lagged Value';
data test;
input x @@;
if mod(x,2)=0 then a=lag(x);
b=lag(x);
if mod(x,2)=0 then c=b;
label a='(WRONG) a' c='(RIGHT) c';
datalines;
1 2 3 4 5 6 7 8
;
proc print label data=test;
run;
[color=red:bc182]data myset1;
set myset;
test1=lag(stkcd);
test2=lag(accper);
if stkcd=test1 and dif(accper)=10000 then do;
stkcd1=test1;
accper1=test2;
end;
drop test1 test2;
run;[/color:bc182]