BookmarkSubscribeRSS Feed
SAS_inquisitive
Lapis Lazuli | Level 10

 

Hello,

 

Can someone explain me the lag fuction in terms of 'QUEUE'?  Also why conditional lag can give incorrect result?

 

http://www.howles.com/saspapers/CC33.pdf

 

Thanks !

8 REPLIES 8
Kurt_Bremser
Super User

The lag() function works like a FIFO (first-in-first-out) queue. It only gets a value when it is called. Calling it conditionally will result in not all values being fed into the queue, and therefore unwanted results.

SAS_inquisitive
Lapis Lazuli | Level 10

@Kurt_Bremser I wonder if you have an example for this?

Kurt_Bremser
Super User

I leave that as an exercise for the reader. It's really simple to devise code that illustrates this.


@SAS_inquisitive wrote:

@Kurt_Bremser I wonder if you have an example for this?


 

SAS_inquisitive
Lapis Lazuli | Level 10

@Kurt_Bremser Thank you. Looks like this paper has really good examples. 

 

http://www.lexjansen.com/nesug/nesug10/cc/cc22.pdf

SAS_inquisitive
Lapis Lazuli | Level 10

Let's I have a mock data set.

 

data mock;

   input x;

 cards;

1

2

3

;

run;

 

When I execute lag(x), what would be the value in queue 3 or 1? Is there anyway we can see the values in queue?

 

Thanks !

SAS_inquisitive
Lapis Lazuli | Level 10

so here is my understanding, when x = 1, there is nothing in the queue, hence it returns missing value. When X=2, 1 is in the queue and it returns 1. Similarly when x =3, 2 is in the queue, and it returns a value of 2.

Quentin
Super User

Your understanding is correct.  I don't think there s a way to see what is inside the queue, other than pushing values into the queue to push them out. 

 

Definitely take the time to play around with conditional lags, to convince yourself that the lag() function works when called conditionally.  Far too many people mistakenly think there is a bug in the lag function and that it should never be called conditionally. It works perfectly fine conditionally, you just need to understand how a queue works.  e.g., think about what you should expect from:

 

data _null_;
  set mock;
  if x IN (2,3) then lag=lag(x);
  put x= lag=;
run;

 

 

Also worth playing with lags with longer queues, e.g.:

data _null_;
  set mock;
  lag1=lag1(x);
  lag2=lag2(x);
  lag3=lag3(x);
  put (x lag1 lag2 lag3)(=);
run;

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
Kurt_Bremser
Super User

To slightly expand your code and show the problems of a conditional lag, just do this:

data mock;
input x;
cards;
1
2
3
4
5
;
run;

data test;
set mock;
if x ne 3 then x1 = lag(x);
run;

proc print data=test noobs;
run;

The result shows the effect of lag() not being called in obs 3:

x    x1

1     .
2     1
3     .
4     2
5     4

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1180 views
  • 3 likes
  • 3 in conversation