Pass in a "Where" into a %Macro...?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Pass in a "Where" into a %Macro...?

Is it possible to pass in a Where? like...

 

%macro test(where);

proc sql;
   create table temp as
   select field1, field2 from tableA where &where;
quit;

%mend test;

%test("ap_period='1234'")

Accepted Solutions
Solution
‎11-29-2017 11:45 AM
Super User
Posts: 21,546

Re: Pass in a "Where" into a %Macro...?

Yes, but slightly easier if you use WHERE =

 

e.g

 

%macro test(where=);

proc sql;
   create table temp as
   select name, age from sashelp.class where &where;
quit;

%mend test;

%test(where= age=15);

StaticFX wrote:

Is it possible to pass in a Where? like...

 

%macro test(where);

proc sql;
   create table temp as
   select field1, field2 from tableA where &where;
quit;

%mend test;

%test("ap_period='1234'")

 

View solution in original post


All Replies
Regular Contributor
Posts: 245

Re: Pass in a "Where" into a %Macro...?

Yes, but you will need to change your SQL into
where %unquote(&where);
Occasional Contributor
Posts: 19

Re: Pass in a "Where" into a %Macro...?

Posted in reply to AhmedAl_Attar

that simple?

lol guess i shouldve tried it first! Thanks! testing now

Solution
‎11-29-2017 11:45 AM
Super User
Posts: 21,546

Re: Pass in a "Where" into a %Macro...?

Yes, but slightly easier if you use WHERE =

 

e.g

 

%macro test(where=);

proc sql;
   create table temp as
   select name, age from sashelp.class where &where;
quit;

%mend test;

%test(where= age=15);

StaticFX wrote:

Is it possible to pass in a Where? like...

 

%macro test(where);

proc sql;
   create table temp as
   select field1, field2 from tableA where &where;
quit;

%mend test;

%test("ap_period='1234'")

 

Occasional Contributor
Posts: 19

Re: Pass in a "Where" into a %Macro...?

Bingo! the first way didnt quite work.. this works perfectly thanks!!

Super User
Super User
Posts: 7,399

Re: Pass in a "Where" into a %Macro...?

Just wrap your condition inside of parentheses instead of quotes.  

%test((ap_period='1234'))

Also note that you can use named parameters in the macro call, even if the macro has it defined as a positional argument. 

You just can't try to pass a value by position when the parameter is defined as named in the %macro statement.

%test(where=ap_period='1234')
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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