- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If you trace your code through five loops you'll find it's overwriting itself all the time. So what are you trying to do here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content