Help using Base SAS procedures

Ordering Dates (Horizontal Data)

Reply
Occasional Learner
Posts: 1

Ordering Dates (Horizontal Data)

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

PROC Star
Posts: 288

Re: Ordering Dates (Horizontal Data)

I think this will do what you want:

 


data want;
    set have;
    call sortn(of date1 - date4);
run;
Super User
Posts: 5,077

Re: Ordering Dates (Horizontal Data)

[ Edited ]

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!

PROC Star
Posts: 288

Re: Ordering Dates (Horizontal Data)

Note one small typo:

 

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

 

Then it works like a charm.

Super User
Posts: 9,671

Re: Ordering Dates (Horizontal Data)

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;
Ask a Question
Discussion stats
  • 4 replies
  • 164 views
  • 2 likes
  • 4 in conversation