Hi all. I am doing many iterative full joins on different sets of variables. I would like to create a macro to simplify things, but I am not sure how to put the necessary code containing the variables into the macro. Here is a partially macroed version that works:
data data1x; input idx var1x $ var2x $; datalines;
1 moob shmoob
2 groob hoob
3 gloob poob
4 bloob poob
;
run;
data data1y; input idy var1y $ var2y $; datalines;
1 moob shmoob
2 froob bloob
3 gloob poob
4 gloob bloob
;
run;
%macro match (a,b);
proc sql;
create table match&a. as
select *
from data&b.x as x full join data&b.y as y
on x.var1x=y.var1y and x.var2x=y.var2y;
quit;
%mend match;
%match(2,1);
And this is what I would like to do:
%macro match (a,b,matchvars);
proc sql;
create table match&a. as
select *
from data&b.x as x full join data&b.y as y
on &matchvars.;
quit;
%mend match;
%match(2,1,x.var1x=y.var1y and x.var2x=y.var2y);
However, I get this error: "ERROR: Invalid macro parameter name x.var1x. It should be a valid SAS identifier no longer than 32 characters."
How can I successfully put this complex string of text into the macro? Thank you!
just use %str to quote the string in macro. Check my codes with some typo correction.
%macro match (a,b,matchvars); proc sql; create table match&a. as select * from data&b.x as x full join data&b.y as y on &matchvars.; quit; %mend match; %match(2,1,%str(x.var1x=y.var1y and x.var2x=y.var2y));
just use %str to quote the string in macro. Check my codes with some typo correction.
%macro match (a,b,matchvars); proc sql; create table match&a. as select * from data&b.x as x full join data&b.y as y on &matchvars.; quit; %mend match; %match(2,1,%str(x.var1x=y.var1y and x.var2x=y.var2y));
Instead of attempting to pass a value like "x.var1x=y.var1y" use the mnemonic EQ instead of =
such as "x.var1x eq y.var1y"
One of the forms of passing parameters is called keyword and uses varname= value in the parameters.
So when the macro processor sees %somemacro (x.var1x=y.var1y ) as the parameter string it thinks you are attempting to assign the value "y.var1y" to a macro variable named "x.var1x" , which is not a legal name for a macro.
you could also do something like
%match(2,1,%str (x.var1x=y.var1y and x.var2x=y.var2y) );
The %str ( parameter text ) will mask the meaning of some of the characters like =. though not so much if you use % or &.
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.