<?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 calculator giving an error for operator in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793305#M254248</link>
    <description>&lt;DIV class=""&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;amp;op.=+&lt;/DIV&gt;&lt;PRE class=""&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;options mlogic symbolgen mprint&lt;BR /&gt;%macro calc(a=,b=,op=);&lt;BR /&gt;%global c;&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(+) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a.+ &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(-) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a. - &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)= %nrstr(*) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a.* &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(/) %then %do;&lt;BR /&gt;%if &amp;amp;b.=0 %then&lt;BR /&gt;%put "SOORRY";&lt;BR /&gt;%else&lt;BR /&gt;%let C=%eval(&amp;amp;a./ &amp;amp;b.);&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;</description>
    <pubDate>Sat, 29 Jan 2022 19:11:44 GMT</pubDate>
    <dc:creator>ANIRBAN2</dc:creator>
    <dc:date>2022-01-29T19:11:44Z</dc:date>
    <item>
      <title>calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793305#M254248</link>
      <description>&lt;DIV class=""&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;amp;op.=+&lt;/DIV&gt;&lt;PRE class=""&gt;&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;options mlogic symbolgen mprint&lt;BR /&gt;%macro calc(a=,b=,op=);&lt;BR /&gt;%global c;&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(+) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a.+ &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(-) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a. - &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)= %nrstr(*) %then&lt;BR /&gt;%let C=%eval(&amp;amp;a.* &amp;amp;b.);&lt;BR /&gt;%if %str(&amp;amp;op.)=%nrstr(/) %then %do;&lt;BR /&gt;%if &amp;amp;b.=0 %then&lt;BR /&gt;%put "SOORRY";&lt;BR /&gt;%else&lt;BR /&gt;%let C=%eval(&amp;amp;a./ &amp;amp;b.);&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 19:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793305#M254248</guid>
      <dc:creator>ANIRBAN2</dc:creator>
      <dc:date>2022-01-29T19:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793314#M254251</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/414446"&gt;@ANIRBAN2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for sharing the code and error. There are a few things to note with the macro definition, but for starters, how are you calling the macro? Have you tried something like the following?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%calc(a=1,b=2,op=%str(+));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jan 2022 20:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793314#M254251</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2022-01-29T20:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793323#M254254</link>
      <description>&lt;P&gt;Is this just an experiment to see if you can do arithmetic in SAS macro language or is it for some other purpose?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A SAS DATA step is a much better place to do calculations like this and it can still be inside a macro:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mlogic symbolgen mprint
%macro calc(a=,b=,op=);
%global c;

data _null_;
  &amp;amp;c = &amp;amp;a &amp;amp;op &amp;amp;b;
run;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 00:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793323#M254254</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-01-30T00:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793324#M254255</link>
      <description>&lt;P&gt;Numeric operators within a %IF condition cause trouble.&amp;nbsp; The macro processor will look to perform math.&amp;nbsp; Instead of using quoting functions, simply use double quotes so macro language knows you have characters and won't try to perform math:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if "&amp;amp;op" = "+" %then
%let C=%eval(&amp;amp;a.+ &amp;amp;b.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be sure to use double quotes (not single quotes), and make certain that the value of &amp;amp;OP does not contain any leading or trailing blanks.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 00:18:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793324#M254255</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-01-30T00:18:30Z</dc:date>
    </item>
    <item>
      <title>Re: calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793348#M254266</link>
      <description>&lt;P&gt;The reason that the %if statement causes trouble when dealing with character values is that an implicit call to the %Eval Macro Function is performed behind the scenes. As the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n07pr39df9k7m3n1w3x1q09iewta.htm" target="_self"&gt;%Eval Doc&lt;/A&gt; says, it &lt;EM&gt;&lt;STRONG&gt;"Evaluates arithmetic and logical expressions using integer arithmetic."&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can cause various problems. For example, if a values has a dot in it as below. Here, the macro m will conclude that 10.0 is less than 2.0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro m;
   %if 10.0 &amp;gt; 2.0 %then %do;
      %put 10.0 is greater than 2.0!;
   %end;
   %else %do;
      %put 10.0 is less than 2.0!;
   %end;
%mend;
 
%m;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I doubt that you actually really need this macro. However, for a workaround, I would go with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;s solution.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Jan 2022 11:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793348#M254266</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-01-30T11:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: calculator giving an error for operator</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793353#M254271</link>
      <description>This is also Working</description>
      <pubDate>Sun, 30 Jan 2022 12:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculator-giving-an-error-for-operator/m-p/793353#M254271</guid>
      <dc:creator>ANIRBAN2</dc:creator>
      <dc:date>2022-01-30T12:26:20Z</dc:date>
    </item>
  </channel>
</rss>

