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);
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.
Find more tutorials on the SAS Users YouTube channel.