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

We have this sample of dataset:

data ds;
input ID session date1 date2 date 3;
datalines;
A 1 feb1 . .
A 1 feb15 . .
A 2 feb5 . .
A 3 mar23 . .
A 1 . mar23 .
A 1 .. may6
B 1 jan1 . .
B 1 feb3 . .
B 1 . . aug2
B 3 . . dec3
;
run;

We need to combine all dates by ID in one column. This is the output. After, we can sort by ID, session and date. 
ID session date
A 1 feb1
A 1 feb15
A 2 feb5
A 3 mar23
A 1 mar23
A 1 may6
B 1 jan1
B 1 feb3
B 1 aug2
B 3 dec3

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Please do yourself a HUGE favor and work with dates as actual valid numeric SAS variables. Do not work with dates as character strings. Working with dates as numeric values is a great habit to get into and will have huge benefits (in most problems). However, using your character values, you would do this:

 

data want;
    set ds;
    sessiondate=coalescec(date1,date2,date3);
run;

 

I have left the dates as characters, but there really is no way to make additional use of text strings such as feb15. In fact feb15 comes before feb5 in any comparison of text strings. So you cannot sort these by date. I'm sure feb15 coming before feb5 is not a useful sorting for you.

 

So, here is the same problem with numeric SAS date values, assuming these are all 2023. The use of informat :DATE9. turns the text into actual valid numeric SAS date values.

 

data ds;
input ID $ session date1 :date9. date2 :date9. date3 :date9.;
format date1-date3 date9.;
datalines;
A 1 01feb2023 . .
A 1 15feb2023 . .
A 2 05feb2023 . .
A 3 23mar2023 . .
A 1 . 23mar2023 .
A 1 .. 06may2023
B 1 01jan2023 . .
B 1 03feb20233 . .
B 1 . . 02aug2023
B 3 . . 03dec2023
;
data want;
    set ds;
    sessiondate=coalesce(date1,date2,date3);
    format sessiondate date9.;
run;

 

Now you can do arithmetic and logical operations on these numeric dates, and 05FEB2023 comes before 15FEB2023.

By the way, please test your code that provides the sample data. The code you provided does not work. You need to test it and make sure it works before posting your code.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Please do yourself a HUGE favor and work with dates as actual valid numeric SAS variables. Do not work with dates as character strings. Working with dates as numeric values is a great habit to get into and will have huge benefits (in most problems). However, using your character values, you would do this:

 

data want;
    set ds;
    sessiondate=coalescec(date1,date2,date3);
run;

 

I have left the dates as characters, but there really is no way to make additional use of text strings such as feb15. In fact feb15 comes before feb5 in any comparison of text strings. So you cannot sort these by date. I'm sure feb15 coming before feb5 is not a useful sorting for you.

 

So, here is the same problem with numeric SAS date values, assuming these are all 2023. The use of informat :DATE9. turns the text into actual valid numeric SAS date values.

 

data ds;
input ID $ session date1 :date9. date2 :date9. date3 :date9.;
format date1-date3 date9.;
datalines;
A 1 01feb2023 . .
A 1 15feb2023 . .
A 2 05feb2023 . .
A 3 23mar2023 . .
A 1 . 23mar2023 .
A 1 .. 06may2023
B 1 01jan2023 . .
B 1 03feb20233 . .
B 1 . . 02aug2023
B 3 . . 03dec2023
;
data want;
    set ds;
    sessiondate=coalesce(date1,date2,date3);
    format sessiondate date9.;
run;

 

Now you can do arithmetic and logical operations on these numeric dates, and 05FEB2023 comes before 15FEB2023.

By the way, please test your code that provides the sample data. The code you provided does not work. You need to test it and make sure it works before posting your code.

--
Paige Miller
ANKH1
Pyrite | Level 9
Thanks for the rec. In the actual dataset I do have the dates as date variables. coalesce function worked great. Thanks!
PaigeMiller
Diamond | Level 26

@ANKH1 wrote:
Thanks for the rec. In the actual dataset I do have the dates as date variables. coalesce function worked great. Thanks!

Great. From now on, please provide data that actually represents the data in your real problem. Providing data that doesn't represent the data in your real problem wastes our time and wastes your time.

--
Paige Miller

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 520 views
  • 0 likes
  • 2 in conversation