8

Here is the part of my program.(oldindex and oldreadmit is in retain commend)

The problem is it works for oldindex=1 then readmit=1 but doesn't work lag(oldreadmit)=1 then readmit=1. Could you tell what's the problem? Thanks in advance!

else if 0< gap <= 30 then do;
     index_d=0;
     if lag(oldreadmit)=1 or oldindex=1  then readmit=1;
         else oth=1;
     oldindex=index_d; 
     oldreadmit=readmit;
     end;

Jane

4 Answers 4

11

The SAS lag function is a cause for much confusion, but once you understand how it works it makes sense. Suppose you have 3 observations, and you have an if statement that causes the second observation to be skipped during processing. If you then apply the lag function to the third observation, it will return the first observation, not the second, because the last time any observation was processed was for the first observation.

What this means is, be careful when combining lags and if statements. In your code, you have a lag in a clause that will only be executed if an if statement is true. This will give you weird results. What you should do is define a variable, say l_oldreadmit, to equal the lag before using it in the if statement.

This will work:

l_oldreadmit = lag(oldreadmit);

if (... whatever you have here ...);
else if 0< gap <= 30 then do;
 index_d=0;
 if l_oldreadmit=1 or oldindex=1  then readmit=1;
     else oth=1;
 oldindex=index_d; 
 oldreadmit=readmit;
 end;
1
  • Thanks everyone. It is much helpful. Jane Feb 29, 2012 at 14:45
4

Just another tip on using the lag function, from version 9 onwards you can use the IFN and IFC functions which don't fall foul of the problems encountered with a standard IF statement. Look at the results of the following code and you'll see what I mean.

    data test;
    input col1;
    if col1>0 then col2=lag(col1);
    col3=ifn(col1>0,lag(col1),.);
    cards;
    1
    2
    0
    5
    0
    4
    ;
    run;

For a more detailed explanation, here is a good paper on the subject. http://www.howles.com/saspapers/CC33.pdf

0
1

As Itzy says above the unexpected behaviour of the lag function is confusing. Lucky users will have problems with it immediately, unlucky users may go for a long time before realizing there's a problem (if they ever do). For this reason I avoid the lag function completely. It's too easy to use it incorrectly, and I want to protect myself from making mistakes that can be easily avoided. That way I can spend more time coding and less time debugging.

I recommend just using the retain statement. Everybody understands how it works and the relationship between variables, the retain statement and if statements is well understood.

0

The LAG function can be confusing as it does not return the value of a variable from the previous observation, but from a queue. The queue is populated based on when it was last called. An article on the topic -

http://analyticsworld.in/aw/the-perils-of-the-lag-function/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.