Hi,
Data A:
ID Date1 Date 2 Date3
1 20180409 20150506 20140203
2 20170607 20180607 20170607
3 20150809 . 20150810
I want:
ID TRT
1 20140203 ( I want the earliest date to be selected out of 3 dates)
2 20170607 ( I want earliest and more treatment dates)
3 20150809 ( earliest date selected)
Can I do this is SAS?
thank you.
Are variables DATE1 DATE2 and DATE3 numeric or character? If numeric, what format has been applied? Please show us the PROC CONTENTS output for these variables.
@Smitha9 wrote:
numeric and format=20.
Which means you don't have dates, you have some numeric crap that looks like dates. And you have a suboptimal data structure (wide instead of long).
First, transpose to long:
data a;
input ID Date1 Date2 Date3;
datalines;
1 20180409 20150506 20140203
2 20170607 20180607 20170607
3 20150809 . 20150810
;
proc transpose data=a out=long_bad (drop=_name_ rename=(col1=date) where=(date ne .));
by id;
var date:;
run;
Next, make usable dates out of the numeric crap:
data long_good;
set long_bad;
date = input(put(date,z8.),yymmdd8.);
format date yymmdd10.;
run;
Then, apply the proper procedure:
proc summary data=long_good nway;
class id;
var date;
output out=want (drop=_type_ _freq_) min()=;
run;
Earlier dates are smaller than later dates. So use the MIN(,) function.
data want;
set have;
trt = min(of date1-date3);
run;
If your date variables are actual SAS date values (and just look like that in your printout because they are being displayed using the YYMMDDN8. format) then also attach a date type format to the new TRT variable.
Note that your example values do NOT look like dates to me. The look like integer numbers in the style YY,YYM,MDD. But since the digits are in Year, Month, Day order they will still work with the MIN() function.
Data posted in usable form helps us to provide tested code. So here a just some hints to find the solution yourself.
Try this. Modify the output format as needed.
Thank you @Tom. Corrected as suggested.
Data A;
Retain ID earliest_date Date1 Date2 Date3;
Informat date1 date2 date3 anydtdte8.;
Format earliest_date date1 date2 date3 date9. ;*yyyymmdd8n.;
infile datalines truncover;
input ID Date1 Date2 Date3;
earliest_date= min(of date1-date3);
datalines;
1 20180409 20150506 20140203
2 20170607 20180607 20170607
3 20150809 . 20150810
;
run;
The output will be like this
@Sajid01 wrote:
...
earliest_date= min(date1-date3);
When you give the MIN() function only one value like that it just returns the input. So that is the same as running:
earliest_date= date1-date3 ;
which will just be difference in days between the first and last date value.
If you want to use a variable list with a SAS function you need to add the OF keyword.
earliest_date= min(of date1-date3);
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.