Hello,
I am trying to use DO LOOP to merge multiple datasets with different names. But the coding is not working, please help. Thanks.
%let Y1=ulung; %let Y2=uheme; %let Y3=ucancer; %let Y4=utransplant; %let Y5=ugm; %macro merge; %do i = 1 %to 5; data Underlying; set &&Y&i.; run; %end; %mend; %merge;
The set statement will accept multiple data sets.
data Underlying; set ulung uheme ucancer utransplant ugm;
So no macro needed.
If the sets are the only ones in your library that start with a common "stem" you can use the : list operator:
data underlying; set u: ; run;
And if you really must have a macro this makes a SET statement like the first example above.
%macro merge; data Underlying; set %do i = 1 %to 5; &&Y&i. %end; ; /*<= this ; ends the SET statement*/ run; %mend;
Since your macro was generating code like:
data want;
set a;
run;
data want;
set b;
run;
Do you see why your output was always the last data set? And you really need to provide a better description than "is not working".
It is often less than best practice to have macro variables appear in the body of a macro without being parameters. Otherwise it can be pretty hard to find where the values are coming from when "something is not working".
By the way, have you figured out how to use option MPRINT yet? I believe it has been suggested several times as a basic tool for debugging macro issues.
Your macro will create 5 data steps, all writing (and thereby overwriting) the same dataset. So in the end, dataset underlying will hold the contents of ugm.
From the code, I have no idea what you want to achieve, but it sure ain't a merge.
The set statement will accept multiple data sets.
data Underlying; set ulung uheme ucancer utransplant ugm;
So no macro needed.
If the sets are the only ones in your library that start with a common "stem" you can use the : list operator:
data underlying; set u: ; run;
And if you really must have a macro this makes a SET statement like the first example above.
%macro merge; data Underlying; set %do i = 1 %to 5; &&Y&i. %end; ; /*<= this ; ends the SET statement*/ run; %mend;
Since your macro was generating code like:
data want;
set a;
run;
data want;
set b;
run;
Do you see why your output was always the last data set? And you really need to provide a better description than "is not working".
It is often less than best practice to have macro variables appear in the body of a macro without being parameters. Otherwise it can be pretty hard to find where the values are coming from when "something is not working".
By the way, have you figured out how to use option MPRINT yet? I believe it has been suggested several times as a basic tool for debugging macro issues.
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 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.