04-07-2017 02:06 AM
I have this code
input cust_id date:date9. sales;
001 23feb2017 100
001 22jan2016 200
001 23mar2017 300
002 01june2015 200
002 03aug2016 300
002 05april2016 400
if first.cust_id=0 then x =lag(date);
I'm getting a missing vcalue in second observation, where I should get the date of first observation?
Except this it's working fine.
04-07-2017 02:16 AM
the LAG function has to be executed for every row.
Find below the changed code. The LAG function is executed for every row, if we are on the first row of a group we reset the value for the variable x.
data t; input cust_id date:date9. sales; format date date9.; cards; 001 23feb2017 100 001 22jan2016 200 001 23mar2017 300 002 01jun2015 200 002 03aug2016 300 002 05apr2016 400 ; data t1; set t; by cust_id; x =lag(date); if first.cust_id=1 then do; x = .; end; format x date9.; run;
04-07-2017 02:46 AM
To expand on @Bruno_SAS's answer: The lag() function is a First-In-First-Out queue, which is only fed when the function is called. If you call it conditionally, everytime the condition is not met, the current value in the variable will not be fed into the queue. Since you did not call lag() in the first data step iteration, its content was still missing when you called it in the second iteration. You'll also get a wrong value after each first. condition, as you'll still have the value from the previous last. observation.