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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4074 views
  • 5 likes
  • 4 in conversation