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

Suppose to have the following: 

 

data DB;
  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 DB1;
  input ID :$20. Discharge :date9. Combined;
  format Discharge date9.;
cards;
0001 19JUN2017  0001_19JUN2017_1
0001 07SEP2020  0001_07SEP2020_2
0002 17MAR2016  0002_17MAR2016_1
0003 05MAY2016  0003_05MAY2016_1
0003 08FEB2017  0003_08FEB2017_2
0004 22MAR2017  0004_22MAR2017_1
0004 03MAY2017  0004_03MAY2017_2
0004 28MAR2021  0004_28MAR2021_3
;

Is there a way to combine IDs and dates in a new variable and add an incremental number (after sorting by ID and Discharge) as suffix?

Desired output: DB1

 

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Thanks for providing working DATA step code!

 

data want;
    set db;
    combined=catx('_',id,put(discharge,date9.));
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Thanks for providing working DATA step code!

 

data want;
    set db;
    combined=catx('_',id,put(discharge,date9.));
run;
--
Paige Miller
ballardw
Super User

This is includes the "incremental number":

 

data want;
    set db;
    by id;
    if first.id then counter=1;
    else counter+1;
    combined=catx('_',id,put(discharge,date9.),counter);
   drop counter;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2 replies
  • 288 views
  • 1 like
  • 3 in conversation