DATA Step, Macro, Functions and more

Get Date Column

Reply
Contributor
Posts: 59

Get Date Column

  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

Respected Advisor
Posts: 3,124

Re: Get Date Column

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

Contributor
Posts: 59

Re: Get Date Column

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

Respected Advisor
Posts: 3,124

Re: Get Date Column

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

Occasional Contributor
Posts: 12

Re: Get Date Column

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

Ask a Question
Discussion stats
  • 4 replies
  • 196 views
  • 2 likes
  • 3 in conversation