I came across this MCQ.
The correct ans is B and even I read the logic behind this. But can anyone plz explain the same in more simple words. I didn't understand the logic explained in the image below.
From now on, please post text and code as text and not as screen captures. From now on, please provide data as working SAS data step code.
Perhaps these words work better for you: https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820751#M3240...
Basically, if you call LAG in an IF statement, and that branch of the IF statement doesn't execute, then the lagged value doesn't get created and thus the lagged value can't be used by the next observation.
From now on, please post text and code as text and not as screen captures. From now on, please provide data as working SAS data step code.
Perhaps these words work better for you: https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820751#M3240...
Basically, if you call LAG in an IF statement, and that branch of the IF statement doesn't execute, then the lagged value doesn't get created and thus the lagged value can't be used by the next observation.
LAG, and its companion function DIF, are queued functions. That means that when you use Lag inside of an IF, or related conditional code structure, that the "previous value" returned by the function is that the previous time that the IF was true.
So generally the preferred usage is to execute the Lag before any IF so you have the value as needed conditionally and typically drop the lagged variable unless needed later.
The real answer is (C) use the DIF() function instead.
tempdiff=dif(temp);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.