If the ultimate goal is to merge multiple datasets, then you can do it using PROC SQL.
The code below is an excerpt from "PROC SQL: Tips and Translations for Data Step Users" by Susan P Marcella & Gail Jorgensen, which you can find here: https://www.lexjansen.com/nesug/nesug10/hw/hw05.pdf
Instead of this:
proc sort data=L.drinkers; by id; run;
proc sort data=L.smokers; by id; run;
data L.LJSmokeDrinkdata;
merge L.smokers(in=smoke) L.drinkers(in=drink);
by id;
if smoke;
run;
Use this:
proc sql;
create table L.LJSmokeDrinkSQL as
select s.*, d.*
from L.smokers as s join L.drinkers as d
on s.id=d.id;
quit;
Alternatively, you can use PROC DATASETS and the CALL EXECUTE statement within a DATA step to sort datasets.
In the code below, I first create the dataset myMembers, which contains the variable NAME that gives me the names of all Datasets (memtype=data) on the WORK library.
Then, CALL EXECUTE allows me to run the same code on all datasets that have the letter 't' in their name (specified by the WHERE statement).
ods output Members=myMembers;
proc datasets lib=work memtype=data; run;
ods output close;
data _null_; set myMembers;
call execute ('proc sort data=work.'||strip(Name)||'; by Patient_ID; run;');
where find(name,'t','i');
run;
I usually reserve CALL EXECUTE for instances where I have several lines of code to do on at least 5 datasets, or simple code on more than 10 datasets.
Hope this helps! Good luck!
I came up with another method.
If you want to sort all the datasets in a library, then it does make sense to use PROC DATASETS before using CALL EXECUTE.
Otherwise, you can list the datasets in a CARDS statement in the same DATA step as the CALL EXECUTE statement.
data _null_;
input datasets $char50. ;
call execute
("proc sort data=work."||strip(datasets)||
"(keep=cum_freq cum_date rename=(cum_freq="||strip(datasets)||"))"||
"out="||strip(datasets)||";"||
"by ByVariable; run;");
cards;
dataset1
dataset2
dataset3
;run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.