<?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: compare macro variable dates not working in sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537535#M147852</link>
    <description>&lt;P&gt;You can't use formatted dates in this command&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1.&amp;lt;&amp;amp;TESTDT1. &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You would be much better off not formatting the data for use in macro evaluations, and in fact formatting the dates so they are readable by humans is wrong here. You could also create and do the test in your data _null_, resulting in much simpler code, no formatting needed, and in fact formatting is wrong here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    testdt = '01JAN13'd;
    mindt = '23JUL12'd;
    ... additional functions as needed ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you must do the work in a macro variable, it would look something like this (I have chosen to code up a simpler problem than the one you are working on as an example)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testdt = %sysfunc(inputn(2013-01-01,yymmdd10.)); /* Note: no formatting here to date9.) */
%let mindt = %sysfunc(inputn(2012-07-23,yymmdd10.)); /* Note: no formatting here to date9. */

/* Do comparison */
%if &amp;amp;mindt &amp;lt; &amp;amp;testdt %then %put this is true;
%else %put this is false;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 21 Feb 2019 21:23:01 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-02-21T21:23:01Z</dc:date>
    <item>
      <title>compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537525#M147845</link>
      <description>&lt;P&gt;I need help on comparing two macro variable dates. Below is the code. You can see by the log that MINDT_AS_DATE_MINUS_ONE_DT1 is 23JUL2012 and TESTDT1 is 01JAN2013. However, when comparing SAS says 23JUL2012 &amp;lt;01JAN2013. is FALSE. I am very confused. Thanks&lt;/P&gt;&lt;PRE&gt;Removed&lt;/PRE&gt;&lt;P&gt;Not working in LOG, See:&lt;/P&gt;&lt;PRE&gt;Removed&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2019 15:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537525#M147845</guid>
      <dc:creator>user112a2</dc:creator>
      <dc:date>2019-04-26T15:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537530#M147847</link>
      <description>&lt;P&gt;Because you're doing a text comparison. SAS doesn't recognize those values as dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can either not use a format and compare the raw numbers within %EVAL or %SYSEVALF or you can use quotation marks and a D to make them date literals in the comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Date literals:&lt;/P&gt;
&lt;PRE&gt;%macro intermediate;
%if &lt;FONT size="4" color="#FF0000"&gt;&lt;STRONG&gt;%sysevalf("&amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1."d &amp;lt; "&amp;amp;TESTDT1."d)&lt;/STRONG&gt; &lt;/FONT&gt;%then %do; 
	%PUT this is true;
%end;
%else %do;
	%PUT this is false;
%end;
%mend intermediate;

%intermediate;&lt;/PRE&gt;
&lt;P&gt;No formats option:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let TESTDT=2013-01-01;
%let TESTDT1=%sysfunc(inputn(&amp;amp;TESTDT, yymmdd10.));
%LET MINDT = 2012-07-23;
%LET MINDT_AS_DATE = %SYSFUNC(INPUTN(&amp;amp;MINDT., YYMMDD10.));
%LET MINDT_AS_DATE_MINUS_ONE = %SYSFUNC(INTNX(MONTHS, &amp;amp;MINDT_AS_DATE., -1));
%LET MINDT_AS_DATE_MINUS_ONE_DT = %SYSFUNC(INPUTN(&amp;amp;MINDT., YYMMDD10.));


data _null_;
MINDT_AS_DATE_MINUS_ONE_DT=&amp;amp;MINDT_AS_DATE_MINUS_ONE_DT.;
call symput('MINDT_AS_DATE_MINUS_ONE_DT1',MINDT_AS_DATE_MINUS_ONE_DT);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225082"&gt;@user112a2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I need help on comparing two macro variable dates. Below is the code. You can see by the log that MINDT_AS_DATE_MINUS_ONE_DT1 is 23JUL2012 and TESTDT1 is 01JAN2013. However, when comparing SAS says 23JUL2012 &amp;lt;01JAN2013. is FALSE. I am very confused. Thanks&lt;/P&gt;
&lt;PRE&gt;%let TESTDT=2013-01-01;
%let TESTDT1=%sysfunc(inputn(&amp;amp;TESTDT, yymmdd10.), date9.);
%LET MINDT = 2012-07-23;
%LET MINDT_AS_DATE = %SYSFUNC(INPUTN(&amp;amp;MINDT., YYMMDD10.));
%LET MINDT_AS_DATE_MINUS_ONE = %SYSFUNC(INTNX(MONTHS, &amp;amp;MINDT_AS_DATE., -1));
%LET MINDT_AS_DATE_MINUS_ONE_DT = %SYSFUNC(INPUTN(&amp;amp;MINDT., YYMMDD10.));
data _null_;
MINDT_AS_DATE_MINUS_ONE_DT=&amp;amp;MINDT_AS_DATE_MINUS_ONE_DT.;
call symput('MINDT_AS_DATE_MINUS_ONE_DT1',put(MINDT_AS_DATE_MINUS_ONE_DT,date9.));
run;
%macro intermediate;
%if &amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1.&amp;lt;&amp;amp;TESTDT1. %then
%do; 
%PUT this is true;
%end;
%else 
%do;
%PUT this is false;
%end;
%mend intermediate;
%intermediate;&lt;/PRE&gt;
&lt;P&gt;Not working in LOG, See:&lt;/P&gt;
&lt;PRE&gt;SYMBOLGEN: Macro variable MINDT_AS_DATE_MINUS_ONE_DT1 resolves to 23JUL2012
SYMBOLGEN: Macro variable TESTDT1 resolves to 01JAN2013
MLOGIC(INTERMEDIATE): %IF condition &amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1.&amp;lt;&amp;amp;TESTDT1. is FALSE
MLOGIC(INTERMEDIATE): %PUT this is false
this is false&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 21:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537530#M147847</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-21T21:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537531#M147848</link>
      <description>&lt;P&gt;Macro variables hold text, not numeric values.&amp;nbsp; So your comparison looks at the "2" at the beginning of "&lt;SPAN&gt;23JUL2012" and compares it to the "0" at the beginning of "01JAN2013".&amp;nbsp; Since "2" is greater than "0", the first date is larger than the second date.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;To get macro language to make a numeric comparison, you could use:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%if %sysevalf("&amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1"d) &amp;lt; %sysevalf("&amp;amp;TESTDT1"d) %then %do;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;That translates the date expressions into numeric values on SAS's date scale.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;looks like you are 1 minute quicker than I am!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 21:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537531#M147848</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-21T21:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537533#M147850</link>
      <description>&lt;P&gt;You don't compare dates, but the strings "23JUL2012" and "01JAN2013" and 2 has a higher value in the ASCII table than 0, so 23JUL2012 &amp;lt; 01JAN2013 is false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you explain what you want to achieve, i am sure that the community will find a solution that works well without macro-code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Well, i am to late again &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 21:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537533#M147850</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-02-21T21:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537535#M147852</link>
      <description>&lt;P&gt;You can't use formatted dates in this command&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1.&amp;lt;&amp;amp;TESTDT1. &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You would be much better off not formatting the data for use in macro evaluations, and in fact formatting the dates so they are readable by humans is wrong here. You could also create and do the test in your data _null_, resulting in much simpler code, no formatting needed, and in fact formatting is wrong here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
    testdt = '01JAN13'd;
    mindt = '23JUL12'd;
    ... additional functions as needed ...
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you must do the work in a macro variable, it would look something like this (I have chosen to code up a simpler problem than the one you are working on as an example)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let testdt = %sysfunc(inputn(2013-01-01,yymmdd10.)); /* Note: no formatting here to date9.) */
%let mindt = %sysfunc(inputn(2012-07-23,yymmdd10.)); /* Note: no formatting here to date9. */

/* Do comparison */
%if &amp;amp;mindt &amp;lt; &amp;amp;testdt %then %put this is true;
%else %put this is false;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Feb 2019 21:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537535#M147852</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-21T21:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537536#M147853</link>
      <description>&lt;P&gt;Looks like it is working to me. The digit 2 is larger than the digit 0 in the lexical ordering of strings.&lt;/P&gt;
&lt;P&gt;If you want SAS to treat those as dates you could convert them to date literals, but then you would need to use %SYSEVALF() if you wanted the macro processor to understand them.&lt;/P&gt;
&lt;PRE&gt;1741  %let MINDT_AS_DATE_MINUS_ONE_DT1=23JUL2012;
1742  %let TESTDT1=01JAN2013;
1743  %IF %sysevalf("&amp;amp;MINDT_AS_DATE_MINUS_ONE_DT1"d &amp;lt; "&amp;amp;TESTDT1"d) %then %do;
1744    %put TRUE;
TRUE
1745  %end;
1746  %else %do;
1747    %put FALSE;
1748  %end;
&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Feb 2019 21:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537536#M147853</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-02-21T21:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: compare macro variable dates not working in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537537#M147854</link>
      <description>Thanks!! Worked like a charm.</description>
      <pubDate>Thu, 21 Feb 2019 21:21:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-macro-variable-dates-not-working-in-sas/m-p/537537#M147854</guid>
      <dc:creator>user112a2</dc:creator>
      <dc:date>2019-02-21T21:21:52Z</dc:date>
    </item>
  </channel>
</rss>

