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;

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!

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.

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
  • 2 replies
  • 727 views
  • 0 likes
  • 3 in conversation