<?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 Where Option in set statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401954#M97555</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I have this below working code working:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro where_clause(where=);
	%if &amp;amp;where.= %then %do;
		%let where_optn= ;
	%end;
	%else %do;
		%let where_optn=(where=(&amp;amp;where.)) ;
	%end;
data class;
set sashelp.class &amp;amp;where_optn.;
run;
%mend;

%where_clause;&lt;BR /&gt;%where_clause(where=name="Alice" );&lt;BR /&gt;&lt;BR /&gt;%where_clause(where=name="Alice" and sex="F" );&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The same code would not run if I invoke the macro as:&lt;/P&gt;
&lt;PRE&gt;%where_clause(where=name in ("Alice") );&lt;/PRE&gt;
&lt;P&gt;Now I understand that it is because of parenthesis. I want the user to be able to pass more conditions using and/or.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Fri, 06 Oct 2017 21:23:02 GMT</pubDate>
    <dc:creator>ArpitSharma</dc:creator>
    <dc:date>2017-10-06T21:23:02Z</dc:date>
    <item>
      <title>Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401954#M97555</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I have this below working code working:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro where_clause(where=);
	%if &amp;amp;where.= %then %do;
		%let where_optn= ;
	%end;
	%else %do;
		%let where_optn=(where=(&amp;amp;where.)) ;
	%end;
data class;
set sashelp.class &amp;amp;where_optn.;
run;
%mend;

%where_clause;&lt;BR /&gt;%where_clause(where=name="Alice" );&lt;BR /&gt;&lt;BR /&gt;%where_clause(where=name="Alice" and sex="F" );&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The same code would not run if I invoke the macro as:&lt;/P&gt;
&lt;PRE&gt;%where_clause(where=name in ("Alice") );&lt;/PRE&gt;
&lt;P&gt;Now I understand that it is because of parenthesis. I want the user to be able to pass more conditions using and/or.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please advise.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401954#M97555</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-10-06T21:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401965#M97564</link>
      <description>&lt;P&gt;You will need to consider all of the cases of the garbage your users may through at your macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%where_clause(where=%str(name="Alice") );
&lt;/PRE&gt;
&lt;P&gt;%str will keep some elements from being seen by the macro parser. But you will need to consider different approaches people are going to attempt to pass Macro elements such as % or &amp;amp; .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%nrstr will mask other characters from interpretation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please see the documentation for these functions as well as %quote, %nrquote, %unquote and %superq and "Macro masking" This is not a trivial issue.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:36:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401965#M97564</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-06T21:36:47Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401970#M97568</link>
      <description>&lt;P&gt;Does it have to be that complicated?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro where_clause(cFilter=);
%if &amp;amp;cFilter=%str() %then %let cFilter=1=1;

data class;
set sashelp.class (where=(&amp;amp;cFilter));
run;

%mend;

%where_clause(cFilter=);
%where_clause(cfilter=name="Alice" );
%where_clause(cFilter=name="Alice" and sex="F" );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I changed the parameter name to help avoid confusion both from a programmer side and in interpreting the model.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401970#M97568</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-06T21:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401971#M97569</link>
      <description>&lt;P&gt;I don't understand your problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code you posted should work as long as the users pass in valid syntax. You will not even need any macro quoting as anything that is valid inside of a WHERE=(....) dataset option will be valid to pass into the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have examples of things that do not work?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:48:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401971#M97569</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-06T21:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401972#M97570</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;there's too many WHEREs in the code. In the parameter and the data step filter.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401972#M97570</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-06T21:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401973#M97571</link>
      <description>This will fail for::&lt;BR /&gt;%where_clause(cFilter=name in ("Alice"));</description>
      <pubDate>Fri, 06 Oct 2017 21:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401973#M97571</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-10-06T21:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401974#M97572</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;This will fail for::&lt;BR /&gt;%where_clause(cFilter=name in ("Alice"));&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRIKE&gt;Nope.&lt;/STRIKE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;371 %where_clause(cfilter=name="Alice" );&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NOTE: There were 1 observations read from the data set&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; SASHELP.CLASS.&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; WHERE name='Alice';&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: The data set WORK.CLASS has 1 observations and 5 variables.'&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: DATA statement used (Total process time):&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; real time 0.02 seconds&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt; cpu time 0.01 seconds&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;EDIT: My bad, you're right.&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401974#M97572</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-06T21:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401975#M97573</link>
      <description>I have provided the example that does NOT work.&lt;BR /&gt;It does NOT work for::&lt;BR /&gt;%where_clause(where=name in ("Alice") );</description>
      <pubDate>Fri, 06 Oct 2017 21:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401975#M97573</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-10-06T21:50:42Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401976#M97574</link>
      <description>I am using the IN operator.</description>
      <pubDate>Fri, 06 Oct 2017 21:52:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401976#M97574</guid>
      <dc:creator>ArpitSharma</dc:creator>
      <dc:date>2017-10-06T21:52:14Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401977#M97575</link>
      <description>&lt;P&gt;It is not the SAS code that is failing it is the invalid %IF condition.&lt;/P&gt;
&lt;P&gt;I would recode it this way.&amp;nbsp; There are slightly more robust methods to test for empty parameters, but %LENGTH() works for 99% of real cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro where_clause(where=);
%local dsnopt;
%if %length(&amp;amp;where) %then %let dsnopt=where=(&amp;amp;where);
data class;
  set sashelp.class (&amp;amp;dsnopt);
run;
%mend where_clause;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2017 21:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/401977#M97575</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-10-06T21:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402315#M97668</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325"&gt;@ArpitSharma&lt;/a&gt;:&lt;/P&gt;&lt;P&gt;A simple solution that will work in most cases:&lt;/P&gt;&lt;PRE&gt;%macro where_clause(where=);
%if %length(&amp;amp;where) %then
  %let where=(where=(&amp;amp;where));
data class;
set sashelp.class &amp;amp;where;
run;
%mend;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Oct 2017 11:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402315#M97668</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-10-09T11:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402424#M97707</link>
      <description>Would add&lt;BR /&gt;%local where;&lt;BR /&gt;after %macro statement. To avoid funny error if the variable where is used outside of where_clause, too.</description>
      <pubDate>Mon, 09 Oct 2017 16:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402424#M97707</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2017-10-09T16:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402425#M97708</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;, because the macro variable WHERE is defined as&amp;nbsp;a parameter to the macro, it is automatically local.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %let where=0;
2    %macro test(where=);
3      %put _user_;
4    %mend test;
5
6    %test(where=1)
TEST WHERE 1
GLOBAL WHERE 0
&lt;/PRE&gt;
&lt;P&gt;I imagine some people might have the practice of declaring all macro variables that are&amp;nbsp;local on a %LOCAL statement, including parameters, but it's not necessary to include the parameters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other than parameters, I'm a zealot about the %LOCAL statement.&amp;nbsp; Nothings bugs me more than to see a post or worse yet paper where macro programmers ignore the need to define the scope of macro variables they create.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 16:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402425#M97708</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-10-09T16:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: Where Option in set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402502#M97753</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;, because the macro variable WHERE is defined as&amp;nbsp;a parameter to the macro, it is automatically local.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Don't know why i haven't noticed that WHERE is a macro parameter. Thanks for clarifying my post.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 20:15:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Where-Option-in-set-statement/m-p/402502#M97753</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2017-10-09T20:15:35Z</dc:date>
    </item>
  </channel>
</rss>

