BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
POOJA_J
Obsidian | Level 7

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. 

POOJA_J_0-1698329778196.pngPOOJA_J_1-1698329796670.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
POOJA_J
Obsidian | Level 7
Thanks for explaining.
I will take care not to post screen shots next time.
ballardw
Super User

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.

 

 

Tom
Super User Tom
Super User

The real answer is (C) use the DIF() function instead.

tempdiff=dif(temp);

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 893 views
  • 3 likes
  • 4 in conversation