Hi,
I'm working with a dataset that includes a list of dates horizontally listed and would like to order them chronologically.
Have:
Date 1 Date 2 Date 3 Date 4
Person 1 7/18/2015 7/15/2015 7/27/2015 7/18/2015
Person 2 5/03/2015 5/04/2015 5/10/2015
Person 3 6/10/2015 6/12/2015
Person 4 11/12/2015 11/14/2015 11/30/2015 11/07/2015
Want:
Date 1 Date 2 Date 3 Date 4
Person 1 7/15/2015 7/18/2015 7/18/2015 7/27/2015
Person 2 5/03/2015 5/04/2015 5/10/2015
Person 3 6/10/2015 6/12/2015
Person 4 11/07/2015 11/12/2015 11/14/2015 11/30/2015
I think this will do what you want:
data want;
set have;
call sortn(of date1 - date4);
run;
One of the key questions is what your date variables contain. If they are character strings, more drastic measures are necessary. But if they are legitimate SAS dates, you can start with SORTN. Unfortunately, that only does half the job. It will treat missing values as the lowest possible value, leaving them on the left instead of the right. A full solution for legitimate SAS dates might be:
data want;
set have;
call sortn(of date1-date4);
changed_date=0;
array date {4};
do _n_=1 to 4;
if date{_n_} > . then do;
changed_date + 1;
if changed_date < _n_ then do;
date{changed_date} = date{_n_};
date{_n_}=.;
end;
end;
end;
drop changed_date;
run;
Thanks!
Note one small typo:
change_date + 1; ----> changed_date + 1;
Then it works like a charm.
data Have;
input id $ (Date1 Date2 Date3 Date4) (: mmddyy10.);
format Date1 Date2 Date3 Date4 mmddyy10.;
cards;
Person1 7/18/2015 7/15/2015 7/27/2015 7/18/2015
Person2 5/03/2015 . 5/04/2015 5/10/2015
Person3 . . 6/10/2015 6/12/2015
Person4 11/12/2015 11/14/2015 11/30/2015 11/07/2015
;
run;
data want;
set have;
array x{*} date1-date4;
array y{*} _date1-_date4;
call sortn(of date1-date4);
n=0;
do i=1 to dim(x);
if not missing(x{i}) then do;n+1;y{n}=x{i};end;
end;
drop i n date1-date4;
format _Date1-_Date4 mmddyy10.;
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.