Hello folks, I believe this is an easy one but somehow i can't figure out the macro to do it. I want to create 4 new datasets by specifying the value of a variable in an existing dataset. Below is my code which is very lengthy and redundant.
data rcst1; set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status); fy="&fy1two.";
where status in ('1','2'); run;
data rcst2; set lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status); fy="&fy2two.";
where status in ('1','2'); run;
data rcst3; set lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status); fy="&fy3two.";
where status in ('1','2'); run;
data rcst4; set lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status); fy="&fy4two.";
where status in ('1','2'); run;
*combine/append all 4 new dataset into one;
data rcstall;
set rcst1 rcst2 rcst3 rcst4; run;
Two things I wonder whether i can achieve:
1. since the existing macro starts from 1 to 4, can i create another loop to go from 1 to 4 instead of copying and pasting so many lines?
2. can I do one set command to append all four new datasets while adding a new variable "fy" to indicate the source of each dataset?
Appreciate any feedback you may have. Thanks!
data rcstall;
set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status)
INDSNAME = src;
*can use text operations to parse this to desired format;
fy = src;
where status in ('1', '2');
run;
data rcstall;
set lcmf.cm20&fy1two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy2two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy3two.01a (keep=UCI RCAbrv Status)
lcmf.cm20&fy4two.01a (keep=UCI RCAbrv Status)
INDSNAME = src;
*can use text operations to parse this to desired format;
fy = src;
where status in ('1', '2');
run;
Thanks @Reeza this works perfectly!
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.