<?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 date variable inside macro loop with %if condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829013#M327503</link>
    <description>&lt;P&gt;The normal %EVAL() macro function used by the %IF does not understand date literals.&lt;/P&gt;
&lt;P&gt;Use %SYSEVALF() if you need to compare date literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %sysevalf(&amp;amp;date. = "&amp;amp;date_last"d ) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or put the UNFORMATTED value of the variable DATE_LAST into the macro variable DATE_LAST and then you won't need to use date literals.&amp;nbsp; So change the SQL query to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint
select date_last format=32. into :date_last trimmed
from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then change all of the "&amp;amp;date_last"d to just &amp;amp;date_last .&lt;/P&gt;</description>
    <pubDate>Wed, 17 Aug 2022 13:09:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-17T13:09:59Z</dc:date>
    <item>
      <title>Macro date variable inside macro loop with %if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829002#M327496</link>
      <description>&lt;P&gt;I have a table of "end dates" that I want to loop over "j" times inside a macro that includes an %if date = INTNX('month',end_date,&amp;amp;j) condition. For some reason the %if condition for j=0 is not evaluated as "true" even though the condition appears to hold if I test it within a separate data step.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
format date_last date9.;
input date_last date9.;
datalines;
01JUL2022
run;

proc sql;
select date_last into :date_last
from have
;
quit;

%macro want;
%do j = 0 %to 0;
%let date = %sysfunc(intnx(month,"&amp;amp;date_last"d,&amp;amp;j.,b));

	data test;
	date = &amp;amp;date.;
	date_last = "&amp;amp;date_last"d;
	if &amp;amp;date. = "&amp;amp;date_last"d then test = 1;
	run;

	%if &amp;amp;date. = "&amp;amp;date_last"d %then %do;
		data want;
		test = 1;
		run;
	%end;
%end;
%mend;
%want; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 12:18:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829002#M327496</guid>
      <dc:creator>ChristianWI</dc:creator>
      <dc:date>2022-08-17T12:18:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro date variable inside macro loop with %if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829005#M327498</link>
      <description>&lt;P&gt;Remove all macro-code and write it down with normal sas code for at least one case. Than post that code and we see how to generalize it. Moving data into macro variables leads almost always to unnecessary complex code.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 12:35:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829005#M327498</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-17T12:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro date variable inside macro loop with %if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829007#M327499</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;date. = "&amp;amp;date_last"d then test = 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This compares the text string &amp;amp;date (which has value 22827) to the text string "&amp;amp;date_last"d which has value "01JUL2022"d. These text strings do not match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do not want formatted value of macro variables when the values are dates. (See &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 28&lt;/A&gt;) You can fix this by un-formatting &amp;amp;date_last (done in the DATA step that creates HAVE) and replacing "&amp;amp;date_last"D with &amp;amp;date_last&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
/* format date_last date9.; */
input date_last date9.;
datalines;
01JUL2022
run;

proc sql;
select date_last format=7. into :date_last
from have
;
quit;
%put &amp;amp;=date_last;

%macro want;
%do j = 0 %to 0;
%let date = %sysfunc(intnx(month,&amp;amp;date_last,&amp;amp;j.,b));
%put &amp;amp;=date;
	data test;
	date = &amp;amp;date.;
	date_last = &amp;amp;date_last;
	if &amp;amp;date. = &amp;amp;date_last then test = 1;
	run;

%put &amp;amp;date=&amp;amp;date_last;
	%if &amp;amp;date. = &amp;amp;date_last %then %do;
		data want;
		test = 1;
		run;
	%end;
%end;
%mend;
%want


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 12:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829007#M327499</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-17T12:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Macro date variable inside macro loop with %if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829013#M327503</link>
      <description>&lt;P&gt;The normal %EVAL() macro function used by the %IF does not understand date literals.&lt;/P&gt;
&lt;P&gt;Use %SYSEVALF() if you need to compare date literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %sysevalf(&amp;amp;date. = "&amp;amp;date_last"d ) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or put the UNFORMATTED value of the variable DATE_LAST into the macro variable DATE_LAST and then you won't need to use date literals.&amp;nbsp; So change the SQL query to:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint
select date_last format=32. into :date_last trimmed
from have
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then change all of the "&amp;amp;date_last"d to just &amp;amp;date_last .&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 13:09:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829013#M327503</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-17T13:09:59Z</dc:date>
    </item>
    <item>
      <title>Re: Macro date variable inside macro loop with %if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829130#M327564</link>
      <description>&lt;P&gt;This worked too. Awesome thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 23:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-date-variable-inside-macro-loop-with-if-condition/m-p/829130#M327564</guid>
      <dc:creator>ChristianWI</dc:creator>
      <dc:date>2022-08-17T23:03:32Z</dc:date>
    </item>
  </channel>
</rss>

