BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Pyrite | Level 9

Hi guys, 

suppose to have the following: 

data DB1;
  input ID Discharge;
  format Discharge date9.;
cards;
0001 19JUN2017
0001 07SEP2020
0002 17MAR2016
0003 05MAY2016
0003 08FEB2017
0004 22MAR2017
0004 03MAY2017
0004 28MAR2021
;

data DB2;
  input ID Discharge Flag NewDate;
  format Discharge NewDate date9.;
cards;
0001 19JUN2017 0    .
0001 07SEP2020 1 08SEP2020
0002 17MAR2016 1 18MAR2016
0003 05MAY2016 0    .
0003 08FEB2017 1 09FEB2017
0004 22MAR2017 0    .
0004 03MAY2017 0    .
0004 28MAR2021 1 29MAR2021
;

Is there a way to add a flag to DB1 where for each ID there's the latest date (if there is only one date as for ID 0002 the last date will be the one reported) and then add the new date corresponding to the last + 1 day? Desired output: DB2.

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

 

data want;
    set db1;
    by id;
    flag=0;
    if last.id then do;
       newdate=discharge + 1;
       flag=1;
    end;
    format newdate date9.;
run;

 

The above assumes that DISCHARGE is an actual valid SAS date value. In the code you provided, it is not an actual valid SAS date value, and so you need to fix the code for DB1.

 

PS: when you provide data as SAS data step code, please test it before you provide it to make sure it works. The code does not work as intended, making all values of DISCHARGE missing.

 

--
Paige Miller

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

 

data want;
    set db1;
    by id;
    flag=0;
    if last.id then do;
       newdate=discharge + 1;
       flag=1;
    end;
    format newdate date9.;
run;

 

The above assumes that DISCHARGE is an actual valid SAS date value. In the code you provided, it is not an actual valid SAS date value, and so you need to fix the code for DB1.

 

PS: when you provide data as SAS data step code, please test it before you provide it to make sure it works. The code does not work as intended, making all values of DISCHARGE missing.

 

--
Paige Miller
NewUsrStat
Pyrite | Level 9
Sorry, I will check and edit soon. Thank you very much!
NewUsrStat
Pyrite | Level 9
Seems to be impossible to edit. No "edit" bottom appears.
ballardw
Super User

@NewUsrStat wrote:
Seems to be impossible to edit. No "edit" bottom appears.

Unless "bottom" is misspelled "button" this doesn't make sense. The three horizontal line icon next to the subject line of your post should have an "edit" option in the drop down menu when clicked.

Patrick
Opal | Level 21

@NewUsrStat wrote:
Seems to be impossible to edit. No "edit" bottom appears.

After some time posts get locked and you can't edit them anymore. Just provide an answer to your own question with the updated code.

NewUsrStat
Pyrite | Level 9

Edit to the code to allow it working correctly: 

 

data DB1;
  input ID :$20.Discharge :date9.;
  format Discharge date9.;
cards;
0001 19JUN2017
0001 07SEP2020
0002 17MAR2016
0003 05MAY2016
0003 08FEB2017
0004 22MAR2017
0004 03MAY2017
0004 28MAR2021
;

data DB2;
  input ID :$20. Discharge :date9. Flag :$20. NewDate:date9.;
  format Discharge NewDate date9.;
cards;
0001 19JUN2017 0    .
0001 07SEP2020 1 08SEP2020
0002 17MAR2016 1 18MAR2016
0003 05MAY2016 0    .
0003 08FEB2017 1 09FEB2017
0004 22MAR2017 0    .
0004 03MAY2017 0    .
0004 28MAR2021 1 29MAR2021
;
Patrick
Opal | Level 21

@NewUsrStat 

Looks like @PaigeMiller already provided the code that returns your desired result. Suggest you mark it as the solution.

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
  • 7 replies
  • 660 views
  • 2 likes
  • 4 in conversation