BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chimei0403
Obsidian | Level 7

Hi SAS experts, 

I need your advice regarding above situation.

I would like to have variables: expect1, expect2, expect3, expect4 by using y_2016, y_2017, y_2018, y_2019 variables.

For expect1 variables, I use coalesce to get this index date. Something like below.

expect1= coalesce(y_2016, y_2017, y_2018, y_2019) ;

but I would like to keep the missing values if there is no information.

(I would like to keep missing values in the row)

In this case, each row represent one patient dates.

Goal is to have t, t+1, t+2, t+3, t+4 information.

Many thanks!

data have ;
  input id y_2016 y_2017 y_2018  y_2019    expect1 expect2 expect3 expect4;
  CARDS;

a   201604  .     .         201901          201604      .        .     201901 
b   .         201708   201811 .             201708    201811     .      .
c    .         .        .     201911        201911      .        .      .
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Figure out where to start. Then copy.

data have ;
  input id $ have1-have4;
cards;
a   201604 .      .      201901       
b   .      201708 201811 .         
c   .      .      .      201911    
d   .      .      .      .
;

data want;
 set have ;
 array old have1-have4;
 array new want1-want4;
 do start=1 to dim(old) until (not missing(old[start])); end;
 target=1;
 do index=start to dim(old);
   new[target]=old[index];
   target+1;
 end;
 drop index target;
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Figure out where to start. Then copy.

data have ;
  input id $ have1-have4;
cards;
a   201604 .      .      201901       
b   .      201708 201811 .         
c   .      .      .      201911    
d   .      .      .      .
;

data want;
 set have ;
 array old have1-have4;
 array new want1-want4;
 do start=1 to dim(old) until (not missing(old[start])); end;
 target=1;
 do index=start to dim(old);
   new[target]=old[index];
   target+1;
 end;
 drop index target;
run;
chimei0403
Obsidian | Level 7
Thank you so much Tom! You are amazing!!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 277 views
  • 1 like
  • 2 in conversation