<?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: error in Macro with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613698#M179262</link>
    <description>&lt;P&gt;Please try to always provide sample data so we can actually run your code. Providing sample data also allows to post desired results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe some quoting around macro variable &amp;amp;condition will resolve the issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
  cc=6000;
  hativa_char=' ';
  niz_crd_max=1;
  output;
  niz_crd_max=2;
  output;
  stop;
run;

%macro mmacro(Field,factor,condition,Run);

  data RawData&amp;amp;Run.;
    set tbl1;
    if hativa_char = 'Other' or CC&amp;gt;5000 then delete;
    Simulation=max(min(&amp;amp;Field.*&amp;amp;factor.,CC),5000);
    %if %nrbquote(&amp;amp;condition) ne %nrbquote() %then
      &amp;amp;condition;
  Run;

%mend mmacro;

options mprint;
/*Run1-Have no error*/
%mmacro(Field=niz_crd_max,
  factor=1.2,
  condition=,
  Run=1);

/*Run2-Have error*/
%mmacro(Field=niz_crd_max,
  factor=1.2,
  condition=%str(IF Simulation&amp;gt;50000 
  AND abs(Simulation-CC)/CC&amp;gt;0.5
  then Simulation=0.5*CC;),
  Run=2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that in your condition in case CC is &amp;lt;=0.5 you'll end up with a division by zero - which will result in SAS throwing an error.&lt;/P&gt;
&lt;P&gt;-&amp;gt; if condition CC&amp;gt;0.5 is False then the returned value is 0&lt;/P&gt;
&lt;P&gt;Eventually consider using the DIVIDE() function documented &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1xybc7e4naq2dn1shl7w9kbxqqz.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
    <pubDate>Tue, 24 Dec 2019 07:07:18 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2019-12-24T07:07:18Z</dc:date>
    <item>
      <title>error in Macro with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613693#M179260</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I&amp;nbsp; am using macro with condition.&lt;/P&gt;
&lt;P&gt;In second run of the macro I get an error&lt;/P&gt;
&lt;P&gt;31 Run=2);&lt;BR /&gt;ERROR: Required operator not found in expression: &amp;amp;condition ^= &lt;BR /&gt;ERROR: The macro mmacro will stop executing.&lt;/P&gt;
&lt;P&gt;May anyone help please to solve it and understand the error&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mmacro(Field,factor,condition,Run);
data RawData&amp;amp;Run.(where=(hativa_char   ne 'Other' 
                                      and CC&amp;gt;5000));
set tbl1;
Simulation=max(min(&amp;amp;Field.*&amp;amp;factor.,CC),5000);
%if &amp;amp;condition ^= %then &amp;amp;condition;&lt;BR /&gt;Run;
%mend mmacro;

/*Run1-Have no error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=,
Run=1);

/*Run2-Have error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=%str(IF Simulation&amp;gt;50000 
			   AND abs(Simulation-CC)/CC&amp;gt;0.5
			   then Simulation=0.5*CC;),
Run=2);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Dec 2019 06:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613693#M179260</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-24T06:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: error in Macro with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613698#M179262</link>
      <description>&lt;P&gt;Please try to always provide sample data so we can actually run your code. Providing sample data also allows to post desired results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe some quoting around macro variable &amp;amp;condition will resolve the issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl1;
  cc=6000;
  hativa_char=' ';
  niz_crd_max=1;
  output;
  niz_crd_max=2;
  output;
  stop;
run;

%macro mmacro(Field,factor,condition,Run);

  data RawData&amp;amp;Run.;
    set tbl1;
    if hativa_char = 'Other' or CC&amp;gt;5000 then delete;
    Simulation=max(min(&amp;amp;Field.*&amp;amp;factor.,CC),5000);
    %if %nrbquote(&amp;amp;condition) ne %nrbquote() %then
      &amp;amp;condition;
  Run;

%mend mmacro;

options mprint;
/*Run1-Have no error*/
%mmacro(Field=niz_crd_max,
  factor=1.2,
  condition=,
  Run=1);

/*Run2-Have error*/
%mmacro(Field=niz_crd_max,
  factor=1.2,
  condition=%str(IF Simulation&amp;gt;50000 
  AND abs(Simulation-CC)/CC&amp;gt;0.5
  then Simulation=0.5*CC;),
  Run=2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that in your condition in case CC is &amp;lt;=0.5 you'll end up with a division by zero - which will result in SAS throwing an error.&lt;/P&gt;
&lt;P&gt;-&amp;gt; if condition CC&amp;gt;0.5 is False then the returned value is 0&lt;/P&gt;
&lt;P&gt;Eventually consider using the DIVIDE() function documented &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p1xybc7e4naq2dn1shl7w9kbxqqz.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 24 Dec 2019 07:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613698#M179262</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-12-24T07:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: error in Macro with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613708#M179268</link>
      <description>&lt;P&gt;There are several ways to check for a null value in a macro variable.&amp;nbsp; There is an entire paper on the subject (Chung &amp;amp; King, I believe).&amp;nbsp; This is my preference:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if %length(&amp;amp;condition) %then &amp;amp;condition;&lt;/P&gt;</description>
      <pubDate>Tue, 24 Dec 2019 09:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/error-in-Macro-with-condition/m-p/613708#M179268</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-12-24T09:17:11Z</dc:date>
    </item>
  </channel>
</rss>

