<?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 TIL: Writing operator AND in a macro as AND or as &amp;amp; does not always give the same result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839570#M331953</link>
    <description>&lt;P&gt;Consider this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t  / minoperator;
  %if 1=1 %then %put a;
  %if 1 in 1 2 %then %put b;
  %if 1=1 &amp;amp; 2=2 %then %put c;
  %if 1 in 1 2 &amp;amp; 2=2 %then %put d;
  %if 1 in 1 2 &amp;amp; 2&amp;gt;2 %then %put e;
  %if 1 in 1 2 and 2&amp;gt;2 %then %put f;
%mend; 
%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output is:&lt;/P&gt;
&lt;P&gt;a&lt;BR /&gt;b&lt;BR /&gt;c&lt;BR /&gt;d&lt;BR /&gt;e&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;e should not be here right?&lt;/P&gt;
&lt;P&gt;The IN operator discounts &lt;STRONG&gt;and&lt;/STRONG&gt; as a keyword, but counts &lt;STRONG&gt;&amp;amp;&lt;/STRONG&gt; as a string in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I know, and you too if you didn't already.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Oct 2022 04:41:44 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2022-10-20T04:41:44Z</dc:date>
    <item>
      <title>TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839570#M331953</link>
      <description>&lt;P&gt;Consider this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t  / minoperator;
  %if 1=1 %then %put a;
  %if 1 in 1 2 %then %put b;
  %if 1=1 &amp;amp; 2=2 %then %put c;
  %if 1 in 1 2 &amp;amp; 2=2 %then %put d;
  %if 1 in 1 2 &amp;amp; 2&amp;gt;2 %then %put e;
  %if 1 in 1 2 and 2&amp;gt;2 %then %put f;
%mend; 
%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output is:&lt;/P&gt;
&lt;P&gt;a&lt;BR /&gt;b&lt;BR /&gt;c&lt;BR /&gt;d&lt;BR /&gt;e&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;e should not be here right?&lt;/P&gt;
&lt;P&gt;The IN operator discounts &lt;STRONG&gt;and&lt;/STRONG&gt; as a keyword, but counts &lt;STRONG&gt;&amp;amp;&lt;/STRONG&gt; as a string in the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I know, and you too if you didn't already.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 04:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839570#M331953</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-10-20T04:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839608#M331961</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Chris ,
You are right .
It seems that '&amp;amp;' is a value for IN operator
You need '()' to tell SAS the range of operator.
That also tells us it always righ to use '()' 
to define range of operator.
Like this :
*/

%macro t  / minoperator mindelimiter=' ' ;
  %if 1=1 %then %put a;
  %if 1 in 1 2 %then %put b;
  %if 1=1 &amp;amp; 2=2 %then %put c;
  %if 1 in 1 2 &amp;amp; 2=2 %then %put d;
  %if (1 in 1 2) &amp;amp; (2&amp;gt;2) %then %put e;
  %if 1 in 1 2 and 2&amp;gt;2 %then %put f;
%mend; 
%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;79   /*
80   Chris ,
81   You are right .
82   It seems that '&amp;amp;' is a value for IN operator
83   You need '()' to tell SAS the range of operator.
84   That also tells us it always righ to use '()'
85   to define range of operator.
86   Like this :
87   */
88
89   %macro t  / minoperator mindelimiter=' ' ;
90     %if 1=1 %then %put a;
91     %if 1 in 1 2 %then %put b;
92     %if 1=1 &amp;amp; 2=2 %then %put c;
93     %if 1 in 1 2 &amp;amp; 2=2 %then %put d;
94     %if (1 in 1 2) &amp;amp; (2&amp;gt;2) %then %put e;
95     %if 1 in 1 2 and 2&amp;gt;2 %then %put f;
96   %mend;
97   %t
a
b
c
d

&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Oct 2022 11:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839608#M331961</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-20T11:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839617#M331968</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;Interesting. Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 11:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839617#M331968</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-10-20T11:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839632#M331972</link>
      <description>&lt;P&gt;You could also just add the parentheses around the list of values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  %if 1 in (1 2) &amp;amp; 2&amp;gt;2 %then %put e;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It never made sense to me why you would do that since the IN operator in macro code does not require it, unlike in regular SAS code.&amp;nbsp; But it does help let the parser know where the list starts and ends.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 12:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839632#M331972</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-20T12:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839641#M331975</link>
      <description>&lt;P&gt;I didn't know, and think maybe it's worth submitting as a bug.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Honestly, there were so many problems in implementing the macro IN operator in the beginning, I've stayed away from it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with your diagnosis: if you don't put parentheses around the argument, the IN operator will treat &amp;amp; and also | as strings, but will treat AND and OR as operators that end the argument.&amp;nbsp; &amp;nbsp;Another test case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t  / minoperator;
  %put 1 in 0 &amp;amp; 1 evaluates to: %eval(1 in 0 &amp;amp; 1) ;     *Returns 1 but should return 0 ;
  %put 1 in 0 and 1 evaluates to: %eval(1 in 0 and 1) ; *Returns 0 ;

  %put 0 in 1 | 0 evaluates to: %eval(0 in 1 | 0) ;     *Returns 1 but should return 0 ;
  %put 0 in 1 or 0 evaluates to: %eval(0 in 1 or 0) ;   *Returns 0 ;
%mend; 

%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks to me like the AND is capable of ending the list of arguments to the IN operator.&amp;nbsp; Below reasonably errors, because SAS can't evaluate the expression 0 1:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t  / minoperator;
  %put  %eval(1 in 0 and 0 1 ) ; *errors, sensibly ;
%mend; 
%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the below executes, and the only way that happens is if the &amp;amp; operator was sucked into the list of arguments for IN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro t  / minoperator;
  %put  %eval(1 in 0 &amp;amp; 0 1 ) ;  *returns 1 ;
%mend; 
%t&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 13:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839641#M331975</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-10-20T13:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839818#M332023</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;Yes&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%if (1 in 1 2) &amp;amp; (2&amp;gt;2) %then %put e;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is the cleanest imho&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Good point, though I don't think I'll use the syntax&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;%if 1 in (1 2) &amp;amp; 2&amp;gt;2 %then %put e;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The parentheses look like they are part of the string values to test. This is confusing to me.&lt;BR /&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;Good set of additional tests, thank you!&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2022 00:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839818#M332023</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-10-21T00:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: TIL: Writing operator AND in a macro as AND or as &amp; does not always give the same result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839900#M332073</link>
      <description>Chris,&lt;BR /&gt;I like Tom's idea. Just take it as 'IN' operator in date step,&lt;BR /&gt;always put parentheses around the list of values , right ?</description>
      <pubDate>Fri, 21 Oct 2022 11:43:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TIL-Writing-operator-AND-in-a-macro-as-AND-or-as-amp-does-not/m-p/839900#M332073</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-10-21T11:43:16Z</dc:date>
    </item>
  </channel>
</rss>

