BookmarkSubscribeRSS Feed
HappySASUE
Quartz | Level 8

Hi I am frustrated with usage of lag. 

Here is the initial dataset: 

 

date1                      flag

2018/05/19              0

2017/01/13             1

2015/04/20              0

2016/09/11              1

2016/12/08             1

2014/07/20              0

2013/11/18              0

 

 

Whenever flag = 1, I want a new variable "newdate" use the previous value: 

 

date1                      flag       newdate

2018/05/19              0          2018/05/19  

2017/01/13              1          2018/05/19  

2015/04/20              0          2015/04/20 

2013/11/18              0          2013/11/18

2016/09/11              1          2013/11/18

2016/12/08              1          2013/11/18

2014/07/20              0          2014/07/20

 

I used: 

if flag = 1 then newdate = lag(date); 

it did not give me what I need. 

 

Do anyone know how to do it? Thanks 

 

 

 

 

5 REPLIES 5
Astounding
PROC Star

I think you're looking for this:

 

data want;

set have;

retain newdate;

if flag=0 then newdate = date1;

run;

Shmuel
Garnet | Level 18

Try:

data want;
 set have;
      retain lag_date;
      lag_date = date1;

     if flag = 1 then newdate = lag_date;
     drop lag_date;
run;
mkeintz
PROC Star

editted note:  overlooked a part of the program.  Corrections made  below.

 

 

The problem is you are looking at the lag function as a "lookback".  But it's really a fifo queue. 

 

Whenever you see the LAG function, in your mind see it as an  "UPDATEFIFO" function.  Then your code would look like:

    if flag=1 then newdate=UPDATEFIFO(date);

 

Using the UPDATEFIFO terminology might make it less counter-intuitive that you didn't get what you expected.  That's because you can see the fifo queue is not being updated with every observation, even though you always want the queue updated, but only sometimes do you want the output of the FIFO queue.


So Instead of

  if flag = 1 then newdate = lag(date1); 

 

You could do this;

   newdate=lag(date1);   /* Always update the queue */

   if flag ^=1 then newdate=date1;   /*But sometimes reject the result*/    /*editted change by mkeintz*/

 

But I would recommend this, which gives the same results:

   newdate=ifn(flag=1,lag(date1),date1);               /*Editted change (TWICE!!) by mkeintz*/

 

The IFN function executes both the 2nd (lag function) and 3rd arguments (date1) even though only one of those results is returned, based on whether the first argument is true.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
HappySASUE
Quartz | Level 8

Thank you so much! 

You explanation really helps.  One more thing: 

newdate=ifn(flag=1,lag(date),date1);               /*Editted change by mkeintz*/

 

do you mean 

newdate=ifn(flag=1,lag(date1),date1);               /*Editted change by mkeintz*/

SuryaKiran
Meteorite | Level 14

Hi,

 

If you use LAG() in the if condition, then value is queued(hold value in memory) when the if condition is satisfied. i.e. when the first if condition is true then the lag value will return missing and not the previous record. 

 

Initiate the LAG() from starting of the records not from middle.

 

data want;
format date1 new_date yymmdd10.;
set have;
lag_date=lag(date1);
if flag=1 then new_date=lag_date;
else new_date=date1;
drop lag_date;
run;
Thanks,
Suryakiran

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 16269 views
  • 3 likes
  • 5 in conversation