<?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 macro in the where clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555776#M154709</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I use a macro variable that has dynamic contents in a where clause.&amp;nbsp; Please see below and pardon my limited knowledge of SAS/macro, Please ignore syntax errors and missing semicolons, if any. But I guess you know where I'm going with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro xx(cond);&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select *&amp;nbsp;&lt;/P&gt;&lt;P&gt;from table1&lt;/P&gt;&lt;P&gt;where &amp;amp;cond;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend xx;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%xx('col1 &amp;gt; 3 and col2 - col3 &amp;gt;= 10');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;basically my question is around the where clause.&amp;nbsp; Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 02 May 2019 21:33:28 GMT</pubDate>
    <dc:creator>jffeudo86</dc:creator>
    <dc:date>2019-05-02T21:33:28Z</dc:date>
    <item>
      <title>macro in the where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555776#M154709</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can I use a macro variable that has dynamic contents in a where clause.&amp;nbsp; Please see below and pardon my limited knowledge of SAS/macro, Please ignore syntax errors and missing semicolons, if any. But I guess you know where I'm going with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro xx(cond);&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select *&amp;nbsp;&lt;/P&gt;&lt;P&gt;from table1&lt;/P&gt;&lt;P&gt;where &amp;amp;cond;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%mend xx;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%xx('col1 &amp;gt; 3 and col2 - col3 &amp;gt;= 10');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;basically my question is around the where clause.&amp;nbsp; Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 21:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555776#M154709</guid>
      <dc:creator>jffeudo86</dc:creator>
      <dc:date>2019-05-02T21:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: macro in the where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555786#M154716</link>
      <description>&lt;P&gt;It does work, i tested on sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro xx(cond);
proc sql;
select * 
from sashelp.class
where &amp;amp;cond;
quit;
%mend xx;

%xx(age&amp;gt;10 and weight&amp;lt;60)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 May 2019 22:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555786#M154716</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-05-02T22:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro in the where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555789#M154719</link>
      <description>&lt;P&gt;You should not have quotation around your in parameter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%xx(col1 &amp;gt; 3 and col2 - col3 &amp;gt;= 10);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Also, to see what is happening with the code, run the following before you do anything else. Should of course only be used during development, not in production.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic symbolgen ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In the log you will then see (if you run the code from jffeudo86):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;MLOGIC(XX): Beginning execution.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;MLOGIC(XX): Parameter COND has value age&amp;gt;10 and weight&amp;lt;60&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;MPRINT(XX): proc sql;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;SYMBOLGEN: Macro variable COND resolves to age&amp;gt;10 and weight&amp;lt;60&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;MPRINT(XX): select * from sashelp.class where age&amp;gt;10 and weight&amp;lt;60;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;MPRINT(XX): quit;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;real time 0.02 seconds&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;cpu time 0.01 seconds&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Compare to your initial code it would look like this:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SYMBOLGEN: Macro variable COND resolves to 'age&amp;gt;10 and weight&amp;lt;60'&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;MPRINT(XX): select * from sashelp.class where 'age&amp;gt;10 and weight&amp;lt;60';&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anything that is not 0 is true, so all rows will be selected (because your string is not 0).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Everything in macro is a string, so you have a string with quotation in it. Similar to a data step variable with the content&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
 var1 ="   &lt;SPAN&gt;'col1 &amp;gt; 3 and col2 - col3 &amp;gt;= 10'   &lt;/SPAN&gt;"; *Spaces added for clarity.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 22:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555789#M154719</guid>
      <dc:creator>heffo</dc:creator>
      <dc:date>2019-05-02T22:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro in the where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555798#M154727</link>
      <description>As you have seen, the answer to your question is, "Of course you can."&lt;BR /&gt;&lt;BR /&gt;One of the skills involved in writing macros is making them flexible.  In this case, should the user be allowed to select all the observations, with no sub setting?  In that case, how would you remove the word "where" from the program?  That's just one thing you would learn along the way.</description>
      <pubDate>Thu, 02 May 2019 22:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555798#M154727</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-05-02T22:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: macro in the where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555904#M154771</link>
      <description>&lt;P&gt;Thank you all for your replies. They are all helpful. I chose this answer for the extra help in seeing what's going on in the background/log.&amp;nbsp; And also the mention of the quotes.&amp;nbsp; No wonder I am getting all the rows instead of those that only meet the condition.&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 12:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-in-the-where-clause/m-p/555904#M154771</guid>
      <dc:creator>jffeudo86</dc:creator>
      <dc:date>2019-05-03T12:50:09Z</dc:date>
    </item>
  </channel>
</rss>

