BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jojozheng
Quartz | Level 8

I'm very new to Macro. I have 5 variables A,B,C,D,E. also I have a code need to repeat, this code used for filling up previous missing value.

data want;
 do until(not missing(status) or last.id );
  set have;
  by id;  
 end;
 temp=status;
 do until(not missing(status) or last.id );
  set have;
  by id;
  _status=temp;output;
 end;
 drop temp status;
run;

I need input variable A into this code, then generate table A, then use tableA as base table, then input variableB into tableA, and so on, until I input last variable E. So final table E will come out, which contains all 5 variables with filling values. 

My logic to get what I want:

data tableA;

set old;

(fill up variableA);

run;

 

data tableB;

set tableA;

(same fill up code to fill up tableB);

run;

.....

data tableE;----------FINAL table filling up all missing values for 5 variables.

set tableD;

(same fill up code tp input variableE);

run;

 

sample data have:

id    varA     B      C      D      E

1       .         .        .       .        .

1      n        m       l       p       k

 

want:

id    varA     B      C      D      E

1      n        m       l       p       k

1      n        m       l       p       k

 

I know how to fill up one by one. I'm thinking if I can use macro to get this final table? or other ways to do that? Thank you for your help! Appreciate! 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are numerous ways of doing it, for example:

data have;
  infile datalines dlm="," dsd;
  input id vara $ varb $ varc $ vard $ vare $;
datalines;
1,,,,,
1,n,m,l,p,k
;
run;

data want;
  update have have (where=(vara ne ""));
  by id;
run;

Or:

data want;
  set have (where=(vara ne ""))
        have (where=(vara ne "");
run;

But i dont think its that straight forward, I can only go on your test data however.  Please post test data in the form of a datastep as I have done above in future.

View solution in original post

6 REPLIES 6
Reeza
Super User
Please show an example of your starting data and what you expect as the end data. Preferably one that aligns so that if we ran the code on the starting data, we could get exactly the end data.
jojozheng
Quartz | Level 8
thank you for suggestion. just edited the post.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are numerous ways of doing it, for example:

data have;
  infile datalines dlm="," dsd;
  input id vara $ varb $ varc $ vard $ vare $;
datalines;
1,,,,,
1,n,m,l,p,k
;
run;

data want;
  update have have (where=(vara ne ""));
  by id;
run;

Or:

data want;
  set have (where=(vara ne ""))
        have (where=(vara ne "");
run;

But i dont think its that straight forward, I can only go on your test data however.  Please post test data in the form of a datastep as I have done above in future.

jojozheng
Quartz | Level 8
thank you for your answer! It works for my data.
Astounding
PROC Star

You can do it all in one easy step, somewhat similar to the @RW9 suggestion:

 

data want;

update have (obs=0) have;

by id;

output;

run;

 

Note that this will affect all variables, not just the five of interest.

jojozheng
Quartz | Level 8
thank you for your help! the code looks more simple !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 995 views
  • 2 likes
  • 4 in conversation