BookmarkSubscribeRSS Feed
Tuecker
Calcite | Level 5

Hi,

I would like to change a dataset that looks like this:


ID   Date1               Date2               Date3               Date4

1    01/05/2014        .                       .                     02/02/2014

2     .                       .                     03/08/2013        .

into this:

ID   NewDate1          NewDate2          NewDate3          etc.

1    01/05/2014           02/02/2014       .         

2    03/08/2013 

I'm not worried about the order of the dates in the particular row, would just like them to 'fill' from the left.

Is this something I can do with an array / retain?

Thanks

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yep, several methods:

data have;

  ID=1; Date1="01MAY2014"d; Date2=.; Date3=.; Date4="02FEB2014"d; output;

  ID=2; Date1=.; Date2=.; Date3="03AUG2014"d; Date4=.; output;

run;

data want (keep=id newdate1-newdate4);

  set have;

  array date{4};

  array newdate{4};

  ind=1;

  do i=1 to 4;

    if date{i} ne . then do;

      newdate{ind}=date{i};

      ind=ind+1;

    end;

  end;

run;

You could also just use if staements if there are just four, or catx all four then split them out etc.

Loko
Barite | Level 11

Hello,

One solution:

data have;
input ID   (Date1-Date4) (:mmddyy10.) ;
format date1-date4 date9.;
datalines;
1 01/05/2014 . . 02/02/2014
2 . . 03/08/2013 .
;
run;


data want;
set have;

array newdate{4};
array date{4};

do i=1 to 4;
if not(missing(date{i})) then
  do;
   j=sum(j,1);
   newdate{j}=date{i};
  end;
end;

drop date: i j;
format newdate: date9.;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1256 views
  • 0 likes
  • 3 in conversation