Since it is the first time that it executed the result of any LAG() function calls in the ELSE part on the third iteration will be missing values.
And each place in the code where you have a LAG function will keep its own queue.
Look at this example.
data x;
do i=1 to 10 ;
lag1=lag(i) ;
if i ne 4 then lag2=lag(i);
put (i lag1 lag2) (=);
end;
run;
i=1 lag1=. lag2=.
i=2 lag1=1 lag2=1
i=3 lag1=2 lag2=2
i=4 lag1=3 lag2=2
i=5 lag1=4 lag2=3
i=6 lag1=5 lag2=5
i=7 lag1=6 lag2=6
i=8 lag1=7 lag2=7
i=9 lag1=8 lag2=8
i=10 lag1=9 lag2=9
Thanks Tom, this is very nice code..
I have a question about lag2 variable, if lag's in the data step follw their own que then lag2 values should be:
lag2=.
lag2=1
lag2=2
lag2=2
lag2=3
lag2=4
lag2=5
lag2=6
lag2=7
lag2=8
lag2=9
but according to ypur result in line 6 in turns to normal flow?
When LAG() is called the top of the stack is returned and the current value is pushed onto the stack.
When I=1 then missing is returned and 1 is pushed.
When I=2 then 1 is returned and 2 is pushed.
When I=3 then 2 is returned and 3 is pushed.
When I=4 it is skipped so LAG2 stays as 2.
When I=5 then 3 is returned and 5 is pushed.
When I=6 then 5 is returned and 6 is pushed.
....
Thanks Tom..
Still i want to ask, should lag function should use same que? i mean this is more meaningfull isnt it?
I come across with this problem while i was searching equivalent of oracle partitioned analytic query ( row_number over (partition by a order by b asc) )
Is there a anoher way to do this?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.