<?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: Why the following code not working? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690979#M210254</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80196"&gt;@sri1&lt;/a&gt;&amp;nbsp; This is a very interesting question that stumped me a few years ago. Basically, what we need to understand is that how a macro %IF %THEN %ELSE treats&lt;EM&gt;&amp;nbsp;&lt;STRONG&gt;INTEGER&amp;nbsp;arithmetic and FLOAT arithmetic.&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The default behavior of a %IF is to compute INTEGER arithmetic/comparison operators. Therefore when the macro processor "tokenises" the code syntax bits that had operators that are of the arithmetic/comparison operator, it evaluates the component by sending signals to the ALU(arithmetic logical unit), places back the result and then continues to process the next subsequent tokens.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, the same %IF is not designed to compute/evaluate FLOAT arithmetic by default and would often result in a similar error that you encountered. Moreover, in your case, the macro processor sees CL, VT and MO as character constant operands and so there is no way that it can compute or evaluate an INTEGER-arithmetic operation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This leads to the situation in us finding a way to treat the operators as text, so that the default behavior of macro processor when dealing with tokens within a %IF is changed to processing regular &lt;EM&gt;&lt;STRONG&gt;name tokens&lt;/STRONG&gt;&lt;/EM&gt; aka &lt;STRONG&gt;text&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro quoting functions, like %STR,%BQUOTE does just that. Of course there is a difference between how stuff works %STR vs%BQUOTE besides the fact former works at compile time and the latter works at execution time with some nuances, but that's beyond the scope of this question in my opinion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the following should fix-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%test(dsn=dsn1,var=%str(CL-VT-MO))

/*OR*/

%test(dsn=dsn1,var=%bquote(CL-VT-MO))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope that helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; for helping me understand this. I guess you both were young back then. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; lol&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Oct 2020 14:06:43 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-10-12T14:06:43Z</dc:date>
    <item>
      <title>Why the following code not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690931#M210222</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following code, but am not getting the desired result. It works only with one condition but not other. Please let me know how to fix this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro test(dsn=,var=);&lt;/P&gt;
&lt;P&gt;data &amp;amp;dsn;&lt;/P&gt;
&lt;P&gt;set&amp;nbsp; core(where=(cat="&amp;amp;var"));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if&amp;nbsp; &amp;amp;var eq&amp;nbsp; CL %then %do;&lt;/P&gt;
&lt;P&gt;proc print data=&amp;amp;dsn;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%else %do;&lt;/P&gt;
&lt;P&gt;proc contents data=&amp;amp;dsn;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro call :&lt;/P&gt;
&lt;P&gt;%test(dsn=dsn1,var=CL); This one is working&lt;/P&gt;
&lt;P&gt;%test(dsn=dsn1,var=CL-VT-MO);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log shows the following error;&lt;/P&gt;
&lt;P&gt;A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.&lt;/P&gt;
&lt;P&gt;The condition was: &amp;amp;cond eq&amp;nbsp; CL&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate your input&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 11:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690931#M210222</guid>
      <dc:creator>sri1</dc:creator>
      <dc:date>2020-10-12T11:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: Why the following code not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690934#M210225</link>
      <description>&lt;P&gt;The SAS macro processor interprets the dash as subtraction, which doesn't make sense in this situation. So, you need to tell the SAS macro processor not to do that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%test(dsn=dsn1,var=%str(CL-VT-MO))&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 11:21:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690934#M210225</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-12T11:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: Why the following code not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690939#M210228</link>
      <description>&lt;P&gt;In a %IF condition, simple arithmetic can be used (in this, the macro processor deviates from being a "pure text" processor). That's why you need to "mask" arithmetic operators.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 11:33:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690939#M210228</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-12T11:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why the following code not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690979#M210254</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80196"&gt;@sri1&lt;/a&gt;&amp;nbsp; This is a very interesting question that stumped me a few years ago. Basically, what we need to understand is that how a macro %IF %THEN %ELSE treats&lt;EM&gt;&amp;nbsp;&lt;STRONG&gt;INTEGER&amp;nbsp;arithmetic and FLOAT arithmetic.&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The default behavior of a %IF is to compute INTEGER arithmetic/comparison operators. Therefore when the macro processor "tokenises" the code syntax bits that had operators that are of the arithmetic/comparison operator, it evaluates the component by sending signals to the ALU(arithmetic logical unit), places back the result and then continues to process the next subsequent tokens.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, the same %IF is not designed to compute/evaluate FLOAT arithmetic by default and would often result in a similar error that you encountered. Moreover, in your case, the macro processor sees CL, VT and MO as character constant operands and so there is no way that it can compute or evaluate an INTEGER-arithmetic operation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This leads to the situation in us finding a way to treat the operators as text, so that the default behavior of macro processor when dealing with tokens within a %IF is changed to processing regular &lt;EM&gt;&lt;STRONG&gt;name tokens&lt;/STRONG&gt;&lt;/EM&gt; aka &lt;STRONG&gt;text&lt;/STRONG&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro quoting functions, like %STR,%BQUOTE does just that. Of course there is a difference between how stuff works %STR vs%BQUOTE besides the fact former works at compile time and the latter works at execution time with some nuances, but that's beyond the scope of this question in my opinion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the following should fix-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%test(dsn=dsn1,var=%str(CL-VT-MO))

/*OR*/

%test(dsn=dsn1,var=%bquote(CL-VT-MO))&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope that helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; for helping me understand this. I guess you both were young back then. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; lol&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Oct 2020 14:06:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690979#M210254</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-12T14:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Why the following code not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690991#M210262</link>
      <description>&lt;P&gt;The problem is this statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if  &amp;amp;var eq  CL %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the solution is shown in the step just above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;dsn;
  set  core(where=(cat="&amp;amp;var"));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just change your test to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if  "&amp;amp;var" eq  "CL" %then %do;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Oct 2020 14:45:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-the-following-code-not-working/m-p/690991#M210262</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-12T14:45:26Z</dc:date>
    </item>
  </channel>
</rss>

