BookmarkSubscribeRSS Feed
omega1983
Calcite | Level 5

  I wrote a proc sql to pull in ID, NAME, LS_DATE1 through 4

proc sql;

create table Test as

select ID,NAME,LS_DATE1,LS_DATE2.........etc

from my_table;

quit;

Some rows will have the LS_DATES populated (Not All).  Each row will usually only have one date in one of the LS_DATE columns.  If there are multiple dates in multiple columns in the same row, I want the last date that appears. I want to get the date and place it in its own GET_DATE column with the same value.  I will use the GET_DATE in calulating the difference in days later.

So I need two examples, one that assumes only one date per column per row and another that looks for the last date in a row if there are multiples.

SAMPLE

ID                                      NAME               LS_DATE1            LS_DATE2             LS_DATE3             LS_DATE4

1                                      Smith                 4/15/12

2                                      Bryant                                              1/12/13

3                                      Jones                  5/1/13

4                                     Thomas                                                                            12/13/06

4 REPLIES 4
Haikuo
Onyx | Level 15

If those dates are numeric, use coalesce(), otherwise use coalescec():

proc sql;

create table Test as

select ID,NAME,coalesce(LS_DATE4,LS_DATE3, LS_DATE2,LS_DATE1) as GET_DATE /*put the last date first*/

from my_table;

quit;

HTH,

Haikuo

omega1983
Calcite | Level 5

data Test

set Test1;

Get_Date=coalesce(LS_DATE1

LS_DATE2 LS_DATE3 Ltr_Dt 😞

run

How can I do the same thing except in a dataset.  All these variables are dates

Haikuo
Onyx | Level 15

If you need a fast pass for solving your imminent issues, you already have one. If you want to learn how it works inside-out, now it is good for you to spend sometime reading the manual:

SAS(R) 9.3 Functions and CALL Routines: Reference

NishunkSaxena
Calcite | Level 5

This is a new function for me Coalasce for numeric and coalescec for string. Read articles realted to it on google and to be frank this is a very helpful function for me......Thanks guys

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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