So my code is
data readms2;
set readms;
PRV_DC_DT = lag(DC_DT);
if _N_=1 then PRV_DC_DT = DC_DT;
format PRV_DC_DT date9.;
PRV_DC_REASON = lag(PRIM_DSCH_REASON);
if _N_=1 then PRV_DC_REASON = PRIM_DSCH_REASON;
run;
I have a unique client ID...
NUM_ADM =0 is the first admission NUM_ADM=1 is a readmission. Essentially when NUM_ADM=0, PRV_DC_DT and PRV_DC_REASON should be missing because they are the first admission so there is not another relevant record to lag from... I'm instead incorrectly getting lagged values from other client IDs when NUM_ADM=0...
What am I missing here... what do I need to make to my code to have the lagged values start on the NUM_ADM=1 record? Thanks in advance.
Resulting, incorrect table is:
data readms2;
set readms;
PRV_DC_DT = lag(DC_DT);
if num_Adm=0 then PRV_DC_DT = .;
format PRV_DC_DT date9.;
PRV_DC_REASON = lag(PRIM_DSCH_REASON);
if num_Adm=0 then PRV_DC_REASON = ' ';
run;
data readms2;
set readms;
PRV_DC_DT = lag(DC_DT);
if num_Adm=0 then PRV_DC_DT = .;
format PRV_DC_DT date9.;
PRV_DC_REASON = lag(PRIM_DSCH_REASON);
if num_Adm=0 then PRV_DC_REASON = ' ';
run;
Thank you Paige. I did add a by statement as well for my unique ID so combined with your suggested addition to my code it seems to have worked. I appreciate the help!
Then bring this topic to a closure and mark Paige's response (or your own explanation) as the solution. It's good to be able to look at a list of topics and know which ones have not yet been marked solved.
It must not have gone through because I did mark as solved.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.