BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kpberger
Obsidian | Level 7

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!

1 ACCEPTED SOLUTION

Accepted Solutions
MINX
Obsidian | Level 7

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));

View solution in original post

3 REPLIES 3
MINX
Obsidian | Level 7

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));
kpberger
Obsidian | Level 7
Perfect, thank you! That works. I edited the post to remove the typos.
ballardw
Super User

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 &.

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1118 views
  • 0 likes
  • 3 in conversation