<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamic Where clause in Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389261#M93335</link>
    <description>It will not work if the user passes where age =12</description>
    <pubDate>Fri, 18 Aug 2017 22:34:40 GMT</pubDate>
    <dc:creator>ArpitSharma</dc:creator>
    <dc:date>2017-08-18T22:34:40Z</dc:date>
    <item>
      <title>Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389240#M93331</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to put a where clause in the sql query if the user passes it in the macro.&lt;/P&gt;
&lt;P&gt;If the user does not pass any macro parameter for the where clause then the query should get executed without any filtering.&lt;/P&gt;
&lt;P&gt;I dont want to use DSID etc options&amp;nbsp;asI have to apply the query on to the database tables also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this but cannot have it working.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro isBlank( param ) ;
  %sysevalf( %superq( param ) =, boolean )
%mend ;

%macro nos_obs(
					dsn	=	 	/*Name of SAS Dataset*/
					where_stmt=abc
					);
%global num_obs	;
	%if %isblank(&amp;amp;where_stmt.)=1 %then %do;
	%let where_cls=;
	%end;
	%else %do;
	%let where_cls=&amp;amp;where_stmt.;	
	%end;

	proc sql noprint;
	select 
		count(*)
			into	:num_obs
	from &amp;amp;dsn.
	&amp;amp;where_cls.
	;quit;
	%put num_obs--&amp;amp;num_obs.;
%mend ;

%nos_obs(dsn=sashelp.class,where_stmt=where age in (12,13));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 21:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389240#M93331</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-18T21:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389246#M93333</link>
      <description>&lt;P&gt;Sure, I think this works but haven't tested it. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Where 1 and &amp;amp;whereClause;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2017 22:08:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389246#M93333</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-18T22:08:09Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389259#M93334</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why are you making it very complicated, the following code will work fine. Is there anything thing difference?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;OPTIONS SYMBOLGEN MPRINT MLOGIC;
%macro nos_obs(dsn=,where_stmt=);

	proc sql noprint;
	select 
		count(*)
			into	:num_obs
	from &amp;amp;dsn.
	&amp;amp;where_stmt.;
	quit;
	%put num_obs--&amp;amp;num_obs.;
%mend ;

%nos_obs(dsn=sashelp.class,where_stmt=where age in (12,13));&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2017 22:30:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389259#M93334</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2017-08-18T22:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389261#M93335</link>
      <description>It will not work if the user passes where age =12</description>
      <pubDate>Fri, 18 Aug 2017 22:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389261#M93335</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-18T22:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389264#M93338</link>
      <description>You're writing the macro. Define the rules. &lt;BR /&gt;If WHERE  is included use SuryaKiran solution.</description>
      <pubDate>Fri, 18 Aug 2017 22:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389264#M93338</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-18T22:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389267#M93339</link>
      <description>&lt;P&gt;It's works fine for me:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %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--&amp;amp;num_obs.
SYMBOLGEN:  Macro variable NUM_OBS resolves to        5
num_obs--       5
MLOGIC(NOS_OBS):  Ending execution.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Aug 2017 22:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389267#M93339</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2017-08-18T22:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389268#M93340</link>
      <description>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</description>
      <pubDate>Fri, 18 Aug 2017 22:50:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389268#M93340</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-08-18T22:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389270#M93342</link>
      <description>&lt;P&gt;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. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Aug 2017 23:14:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389270#M93342</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2017-08-18T23:14:57Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Where clause in Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389302#M93351</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325"&gt;@ArpitSharma&lt;/a&gt; wrote:&lt;BR /&gt;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&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Which version of SAS are you running this on?&lt;/P&gt;</description>
      <pubDate>Sat, 19 Aug 2017 08:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamic-Where-clause-in-Proc-SQL/m-p/389302#M93351</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-19T08:36:57Z</dc:date>
    </item>
  </channel>
</rss>

