%macro try(dsn=);
proc sql;
create table required as
select *
from &dsn.
where condition=x
quit;
%mend try;
I'm able to get the correct result if I call %try(dsn=abc)
What if I have few dsn's and concatenate them by using set statement, within the same above macro.
Thak yoiu.
You could do something like:
data abc;
type='abc';
condition=1;
output;
run;
data def;
type='def';
condition=1;
output;
run;
%macro try(dsn=);
proc sql;
create table required as
select *
from &dsn.
%if %sysfunc(countw(&tdsn.)) gt 1 %then %do i=2 %to %sysfunc(countw(&tdsn.));
union
select *
from %scan(&tdsn.,&i.)
%end;
where condition=1
;
quit;
%mend try;
%try(dsn=abc def)
Art, CEO, AnalystFinder.com
You could do something like:
data abc;
type='abc';
condition=1;
output;
run;
data def;
type='def';
condition=1;
output;
run;
%macro try(dsn=);
proc sql;
create table required as
select *
from &dsn.
%if %sysfunc(countw(&tdsn.)) gt 1 %then %do i=2 %to %sysfunc(countw(&tdsn.));
union
select *
from %scan(&tdsn.,&i.)
%end;
where condition=1
;
quit;
%mend try;
%try(dsn=abc def)
Art, CEO, AnalystFinder.com
It is much easier if you just use a data step instead of trying to use PROC SQL.
%macro try(dsn=);
data required;
set &dsn ;
where condition=x ;
run;
%mend try;
You can then just call it with a space delimited list of dataset names.
%try(dsn=A B C)
Which of course raises the question of why the macro at all as:
%let dsn=abc def; data required; set &dsn.; where conditon=x; run;
Or even:
data required; set abc def; where conditon=x; run;
So the given example is pointless obfucating the code.
And if all your datasets that you want to process in a library start with the same characters :
Data required;
set mylib.abc: ;
where condition=x;
run;
would combine: mylib.abc1 mylib.abc2 mylib.abcpdq;
any of the sets that start with abc.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.