BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

In the following code, I want to execute the where clause in proc print only if the macro variable 'filter' is 'y' otherwise it should not execute.

 

%let target_table=&datasource;
%global prompt;
%filter ='y';

proc sort data= "&envPath/data/gb/&datasource" out=sorted nodupkey /*or noduprecs if sorting all vars*/;
	by &_field /* or list of vars */;
run; 
proc print data= sorted noobs;
	where &parameter_1 in ("&parameter_2"); /** need help here to tweak this line to execute only when the macro variable 'filter' is 'y' */
	var &_field;
run;
2 REPLIES 2
PaigeMiller
Diamond | Level 26
%let filter=y;

proc print data= sorted noobs;
	%if &filter=y %then %do; where &parameter_1 in ("&parameter_2"); %end;
	var &_field;
run;

 

 

Notes: you typed %filter='y'; which is incorrect. You have to use %let as I did, and you should not be enclosing macro variable values in quotes.

 

This assumes that the rest of the WHERE statement is correct, which I don't know, and you haven't shown that &parameter_1 or &parameter_2 have been assigned values.

--
Paige Miller
ballardw
Super User

Why concerned about "executing" the where clause? Does this mean that you  may not always define values for the other macro variables and are looking to avoid other problems because of that?

 

"Not executing" a Where clause is somewhat similar conceptually to "the result of the where is always true" (no filtering is done).

It may be more robust to write such as Where than to invoke the macro processor logic. But no examples of what the other macro variables might look like ...

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 920 views
  • 2 likes
  • 3 in conversation