<?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: How do I use IF with macro based variables arithmetics in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710150#M218553</link>
    <description>&lt;P&gt;There is no need for macro code when you want to do conditional logic on data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro code is for code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this works for you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET VAR1 = JAN07;
%LET VAR2 = JAN06;

DATA HAVE;
 INPUT NAME $ &amp;amp;var1 &amp;amp;var2;
 DATALINES;
aaaaa 100 50
bbbbb 110 60
ccccc 20 15
ddddd 300 290
eeeee 80 79
fffff 115 130
;
RUN;

data want;
   set have;
   if &amp;amp;var1 lt 100 and abs(&amp;amp;VAR2 - &amp;amp;VAR1) ge 5 then output;
   else if &amp;amp;var2 ge 100 and (abs(&amp;amp;VAR2 - &amp;amp;VAR1)) / &amp;amp;VAR2 ge 0.05 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Jan 2021 11:46:34 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-01-08T11:46:34Z</dc:date>
    <item>
      <title>How do I use IF with macro based variables arithmetics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710146#M218550</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;I have a table which variable names are created by macros as per example.&lt;/P&gt;&lt;PRE&gt;%LET VAR1 = JAN07;
%LET VAR2 = JAN06;

DATA HAVE;
 INPUT NAME $ &amp;amp;var1 &amp;amp;var2;
 DATALINES;
aaaaa 100 50
bbbbb 110 60
ccccc 20 15
ddddd 300 290
eeeee 80 79
fffff 115 130
;
RUN;&lt;/PRE&gt;&lt;P&gt;How can I conditionally output from this? I have tried like this but seems like I'm missing something:&lt;/P&gt;&lt;PRE&gt;%MACRO MAC();

data WANT;
SET HAVE;

%IF &amp;amp;var2 LT 100 AND %SYSFUNC(abs(%EVAL(&amp;amp;VAR2 - &amp;amp;VAR1))) GE 5 %then %DO;
output ;
%END;

%else %if &amp;amp;var2 GE 100 AND (%SYSFUNC(abs(%EVAL(&amp;amp;VAR2 - &amp;amp;VAR1))))/&amp;amp;VAR2 GE 0.05 %then %DO;
output;
%END;

run;

%MEND MAC;
%MAC()&lt;/PRE&gt;&lt;P&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:&lt;BR /&gt;JAN06 - JAN07&lt;BR /&gt;ERROR: The function ABS referenced by the %SYSFUNC or %QSYSFUNC macro function has too few arguments.&lt;BR /&gt;ERROR: The macro MAC will stop executing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 11:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710146#M218550</guid>
      <dc:creator>cactooos</dc:creator>
      <dc:date>2021-01-08T11:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use IF with macro based variables arithmetics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710150#M218553</link>
      <description>&lt;P&gt;There is no need for macro code when you want to do conditional logic on data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro code is for code generation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if this works for you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET VAR1 = JAN07;
%LET VAR2 = JAN06;

DATA HAVE;
 INPUT NAME $ &amp;amp;var1 &amp;amp;var2;
 DATALINES;
aaaaa 100 50
bbbbb 110 60
ccccc 20 15
ddddd 300 290
eeeee 80 79
fffff 115 130
;
RUN;

data want;
   set have;
   if &amp;amp;var1 lt 100 and abs(&amp;amp;VAR2 - &amp;amp;VAR1) ge 5 then output;
   else if &amp;amp;var2 ge 100 and (abs(&amp;amp;VAR2 - &amp;amp;VAR1)) / &amp;amp;VAR2 ge 0.05 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Jan 2021 11:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710150#M218553</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-08T11:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use IF with macro based variables arithmetics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710151#M218554</link>
      <description>&lt;P&gt;The macro language statements are compiled (converted to text/SAS code) before the data step code is compiled and&amp;nbsp; executed. So what you are doing will not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, use the variable names in the macro strings to create valid SAS datastep code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  SET HAVE;
if &amp;amp;var2 LT 100 AND abs(&amp;amp;VAR2 - &amp;amp;VAR1) GE 5 then
  output ;
else if abs(&amp;amp;VAR2 - &amp;amp;VAR1)/&amp;amp;VAR2 GE 0.05 then 
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I removed one of the conditions in the second IF statement, as that is already tested in the previous IF statement.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 11:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710151#M218554</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-01-08T11:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use IF with macro based variables arithmetics</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710193#M218573</link>
      <description>Thanks guys&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;</description>
      <pubDate>Fri, 08 Jan 2021 15:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-IF-with-macro-based-variables-arithmetics/m-p/710193#M218573</guid>
      <dc:creator>cactooos</dc:creator>
      <dc:date>2021-01-08T15:57:11Z</dc:date>
    </item>
  </channel>
</rss>

