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
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With