BookmarkSubscribeRSS Feed
shaywort13
Calcite | Level 5

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

4 REPLIES 4
collinelliot
Barite | Level 11

I think this will do what you want:

 


data want;
    set have;
    call sortn(of date1 - date4);
run;
Astounding
PROC Star

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!

collinelliot
Barite | Level 11

Note one small typo:

 

change_date + 1; ---->  changed_date + 1;

 

Then it works like a charm.

Ksharp
Super User
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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 759 views
  • 2 likes
  • 4 in conversation