DATA Step, Macro, Functions and more

Dynamic Where clause in Proc SQL

Reply
Contributor
Posts: 38

Dynamic Where clause in Proc SQL

[ Edited ]

Hello,

 

I want to put a where clause in the sql query if the user passes it in the macro.

If the user does not pass any macro parameter for the where clause then the query should get executed without any filtering.

I dont want to use DSID etc options asI have to apply the query on to the database tables also.

 

I have this but cannot have it working.

%macro isBlank( param ) ;
  %sysevalf( %superq( param ) =, boolean )
%mend ;

%macro nos_obs(
					dsn	=	 	/*Name of SAS Dataset*/
					where_stmt=abc
					);
%global num_obs	;
	%if %isblank(&where_stmt.)=1 %then %do;
	%let where_cls=;
	%end;
	%else %do;
	%let where_cls=&where_stmt.;	
	%end;

	proc sql noprint;
	select 
		count(*)
			into	:num_obs
	from &dsn.
	&where_cls.
	;quit;
	%put num_obs--&num_obs.;
%mend ;

%nos_obs(dsn=sashelp.class,where_stmt=where age in (12,13));

 

Super User
Posts: 19,878

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma

Sure, I think this works but haven't tested it.  

 

Where 1 and &whereClause;
Frequent Contributor
Posts: 141

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma

Hi,

 

Why are you making it very complicated, the following code will work fine. Is there anything thing difference?

OPTIONS SYMBOLGEN MPRINT MLOGIC;
%macro nos_obs(dsn=,where_stmt=);

	proc sql noprint;
	select 
		count(*)
			into	:num_obs
	from &dsn.
	&where_stmt.;
	quit;
	%put num_obs--&num_obs.;
%mend ;

%nos_obs(dsn=sashelp.class,where_stmt=where age in (12,13));

Thanks,
Suryakiran
Contributor
Posts: 38

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma
It will not work if the user passes where age =12
Super User
Posts: 19,878

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma
You're writing the macro. Define the rules.
If WHERE is included use SuryaKiran solution.
Frequent Contributor
Posts: 141

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma

It's works fine for me:

  %nos_obs(dsn=sashelp.class,where_stmt=where age =12);
MLOGIC(NOS_OBS):  Beginning execution.
MLOGIC(NOS_OBS):  Parameter DSN has value sashelp.class
MLOGIC(NOS_OBS):  Parameter WHERE_STMT has value where age =12
MPRINT(NOS_OBS):   proc sql noprint;
SYMBOLGEN:  Macro variable DSN resolves to sashelp.class
SYMBOLGEN:  Macro variable WHERE_STMT resolves to where age =12
MPRINT(NOS_OBS):   select count(*) into :num_obs from sashelp.class where age =12;
MPRINT(NOS_OBS):   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

MLOGIC(NOS_OBS):  %PUT num_obs--&num_obs.
SYMBOLGEN:  Macro variable NUM_OBS resolves to        5
num_obs--       5
MLOGIC(NOS_OBS):  Ending execution.
Thanks,
Suryakiran
Contributor
Posts: 38

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma
We might have to include those strings and other such macros that treats it as string. Even if it has an equal to sign. **Where_stmt=where age =12** will give out error. As the number of positional parameter is different than what is defined
Super User
Posts: 3,261

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma

In my experience named parameters are better that positional ones as it makes it more obvious what has to be added. I usually add a %STR function around the text in complex parameters like WHERE statements as that usually prevents most macro parsing problems.  

Super User
Posts: 11,343

Re: Dynamic Where clause in Proc SQL

Posted in reply to ArpitSharma

ArpitSharma wrote:
We might have to include those strings and other such macros that treats it as string. Even if it has an equal to sign. **Where_stmt=where age =12** will give out error. As the number of positional parameter is different than what is defined

Which version of SAS are you running this on?

Ask a Question
Discussion stats
  • 8 replies
  • 186 views
  • 0 likes
  • 5 in conversation