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 2024

Innovate_SAS_Blue.png

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. 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

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.

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