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

Hi, 

 

Can I use a macro variable that has dynamic contents in a where clause.  Please see below and pardon my limited knowledge of SAS/macro, Please ignore syntax errors and missing semicolons, if any. But I guess you know where I'm going with this.

 

%macro xx(cond);

proc sql;

select * 

from table1

where &cond;

quit;

 

%mend xx;

 

%xx('col1 > 3 and col2 - col3 >= 10');

 

basically my question is around the where clause.  Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
heffo
Pyrite | Level 9

You should not have quotation around your in parameter. 

 

%xx(col1 > 3 and col2 - col3 >= 10);

Also, to see what is happening with the code, run the following before you do anything else. Should of course only be used during development, not in production. 

options mprint mlogic symbolgen ;

In the log you will then see (if you run the code from jffeudo86): 

 

MLOGIC(XX): Beginning execution.
MLOGIC(XX): Parameter COND has value age>10 and weight<60
MPRINT(XX): proc sql;
SYMBOLGEN: Macro variable COND resolves to age>10 and weight<60
MPRINT(XX): select * from sashelp.class where age>10 and weight<60;
MPRINT(XX): quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds

 

Compare to your initial code it would look like this:

SYMBOLGEN: Macro variable COND resolves to 'age>10 and weight<60'
MPRINT(XX): select * from sashelp.class where 'age>10 and weight<60';

 

Anything that is not 0 is true, so all rows will be selected (because your string is not 0). 

 

Everything in macro is a string, so you have a string with quotation in it. Similar to a data step variable with the content 

data x;
 var1 ="   'col1 > 3 and col2 - col3 >= 10'   "; *Spaces added for clarity.;
run;

 

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

It does work, i tested on sashelp.class

 

%macro xx(cond);
proc sql;
select * 
from sashelp.class
where &cond;
quit;
%mend xx;

%xx(age>10 and weight<60)
Thanks,
Jag
heffo
Pyrite | Level 9

You should not have quotation around your in parameter. 

 

%xx(col1 > 3 and col2 - col3 >= 10);

Also, to see what is happening with the code, run the following before you do anything else. Should of course only be used during development, not in production. 

options mprint mlogic symbolgen ;

In the log you will then see (if you run the code from jffeudo86): 

 

MLOGIC(XX): Beginning execution.
MLOGIC(XX): Parameter COND has value age>10 and weight<60
MPRINT(XX): proc sql;
SYMBOLGEN: Macro variable COND resolves to age>10 and weight<60
MPRINT(XX): select * from sashelp.class where age>10 and weight<60;
MPRINT(XX): quit;
NOTE: PROCEDURE SQL used (Total process time):
real time 0.02 seconds
cpu time 0.01 seconds

 

Compare to your initial code it would look like this:

SYMBOLGEN: Macro variable COND resolves to 'age>10 and weight<60'
MPRINT(XX): select * from sashelp.class where 'age>10 and weight<60';

 

Anything that is not 0 is true, so all rows will be selected (because your string is not 0). 

 

Everything in macro is a string, so you have a string with quotation in it. Similar to a data step variable with the content 

data x;
 var1 ="   'col1 > 3 and col2 - col3 >= 10'   "; *Spaces added for clarity.;
run;

 

jffeudo86
Quartz | Level 8

Thank you all for your replies. They are all helpful. I chose this answer for the extra help in seeing what's going on in the background/log.  And also the mention of the quotes.  No wonder I am getting all the rows instead of those that only meet the condition.

Astounding
PROC Star
As you have seen, the answer to your question is, "Of course you can."

One of the skills involved in writing macros is making them flexible. In this case, should the user be allowed to select all the observations, with no sub setting? In that case, how would you remove the word "where" from the program? That's just one thing you would learn along the way.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 4598 views
  • 5 likes
  • 4 in conversation