BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
accintron
Obsidian | Level 7

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:

Capture.PNG

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
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;
--
Paige Miller
accintron
Obsidian | Level 7

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!

mkeintz
PROC Star

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.

--------------------------
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

--------------------------
accintron
Obsidian | Level 7

It must not have gone through because I did mark as solved. 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 752 views
  • 0 likes
  • 3 in conversation