<?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: macro loop %if %then not evaluating correctly in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434717#M107915</link>
    <description>&lt;P&gt;Macro language doesn't understand dates.&amp;nbsp; It understands character strings.&amp;nbsp; If you want to force it to make the "right" comparison, you can do it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if %sysevalf(&amp;amp;t.) ge %sysevalf('01FEB2018'd) %then %do;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Feb 2018 22:18:47 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-02-06T22:18:47Z</dc:date>
    <item>
      <title>macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434712#M107913</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is within a macro function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example termdate&amp;nbsp;= 01/01/2007&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%do II = 0 %to 11;
			%let x = %sysfunc(intnx(month,&amp;amp;termdate.,&amp;amp;ii,B),Date9.);
			%let t = %str(%')&amp;amp;x.%str(%')d;
			%put &amp;amp;t.;
			%put &amp;amp;ii;


					%if %sysevalf(&amp;amp;t.) ge '01FEB2018'd %then %do;&lt;/PRE&gt;&lt;P&gt;the %put &amp;amp;t. produces:&lt;/P&gt;&lt;P&gt;'01JAN2007'd&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but then when I use it in the %if statement&amp;nbsp;below&lt;/P&gt;&lt;P&gt;it evaluates to TRUE when using Mlogic option.&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should be False.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is how it should look when the macro variable has been resolved.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%if '01JAN2007'd ge '01FEB2018'd %then %do;&lt;/PRE&gt;&lt;P&gt;Should be false&amp;nbsp;but it comes out true.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 22:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434712#M107913</guid>
      <dc:creator>mhodge20</dc:creator>
      <dc:date>2018-02-06T22:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434716#M107914</link>
      <description>&lt;P&gt;try :&lt;/P&gt;&lt;PRE&gt;%if %sysevalf(&amp;amp;t. ge '01FEB2018'd) %then %do;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Feb 2018 22:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434716#M107914</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-02-06T22:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434717#M107915</link>
      <description>&lt;P&gt;Macro language doesn't understand dates.&amp;nbsp; It understands character strings.&amp;nbsp; If you want to force it to make the "right" comparison, you can do it with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if %sysevalf(&amp;amp;t.) ge %sysevalf('01FEB2018'd) %then %do;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 22:18:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434717#M107915</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-06T22:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434719#M107917</link>
      <description>&lt;P&gt;Is that all your code? If so, you should use a data step and data step loop, not a macro loop.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/112183"&gt;@mhodge20&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is within a macro function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example termdate&amp;nbsp;= 01/01/2007&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%do II = 0 %to 11;
			%let x = %sysfunc(intnx(month,&amp;amp;termdate.,&amp;amp;ii,B),Date9.);
			%let t = %str(%')&amp;amp;x.%str(%')d;
			%put &amp;amp;t.;
			%put &amp;amp;ii;


					%if %sysevalf(&amp;amp;t.) ge '01FEB2018'd %then %do;&lt;/PRE&gt;
&lt;P&gt;the %put &amp;amp;t. produces:&lt;/P&gt;
&lt;P&gt;'01JAN2007'd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but then when I use it in the %if statement&amp;nbsp;below&lt;/P&gt;
&lt;P&gt;it evaluates to TRUE when using Mlogic option.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be False.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how it should look when the macro variable has been resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if '01JAN2007'd ge '01FEB2018'd %then %do;&lt;/PRE&gt;
&lt;P&gt;Should be false&amp;nbsp;but it comes out true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 22:25:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434719#M107917</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-06T22:25:19Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434720#M107918</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/112183"&gt;@mhodge20&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is within a macro function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example termdate&amp;nbsp;= 01/01/2007&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the %put &amp;amp;t. produces:&lt;/P&gt;
&lt;P&gt;'01JAN2007'd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but then when I use it in the %if statement&amp;nbsp;below&lt;/P&gt;
&lt;P&gt;it evaluates to TRUE when using Mlogic option.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It should be False.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how it should look when the macro variable has been resolved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%if '01JAN2007'd ge '01FEB2018'd %then %do;&lt;/PRE&gt;
&lt;P&gt;Should be false&amp;nbsp;but it comes out true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No it shouldn't. Strings are compared one character at a time until a difference (if any) is found: 0=0, 1=1 but J&amp;gt;F.&lt;/P&gt;
&lt;P&gt;for added fun check on this&lt;/P&gt;
&lt;P&gt;%if '01JAN2007'd ge '01feb2018'd %then %do;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 22:26:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434720#M107918</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-02-06T22:26:14Z</dc:date>
    </item>
    <item>
      <title>Re: macro loop %if %then not evaluating correctly</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434880#M107966</link>
      <description>&lt;P&gt;The condition in a %IF is normally evaluated using the %EVAL() macro function.&amp;nbsp; That function does not handle floating point numbers or literals.&amp;nbsp; So you need to generate your own test using the %SYSEVALF() macro function instead if you are comparing date literals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you need to convert your date like string into a date value, either an actual number of days or a date literal, before you can compare dates.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let datenumber = %sysfunc(intnx(month,&amp;amp;termdate.,&amp;amp;ii,B));
%let dateliteral = "%sysfunc(intnx(month,&amp;amp;termdate.,&amp;amp;ii,B),Date9.)"d;
%if %sysevalf(&amp;amp;datenumber. ge '01FEB2018'd) %then %do;
%if %sysevalf(&amp;amp;dateliteral. ge '01FEB2018'd) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 14:56:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-loop-if-then-not-evaluating-correctly/m-p/434880#M107966</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-07T14:56:41Z</dc:date>
    </item>
  </channel>
</rss>

