DATA Step, Macro, Functions and more

How to search for non-missing values and output them into a new variable

Reply
N/A
Posts: 1

How to search for non-missing values and output them into a new variable

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

Super User
Super User
Posts: 7,970

Re: How to search for non-missing values and output them into a new variable

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.

Super Contributor
Posts: 308

Re: How to search for non-missing values and output them into a new variable

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;

Ask a Question
Discussion stats
  • 2 replies
  • 175 views
  • 0 likes
  • 3 in conversation