<?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 Date Macros in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254969#M48674</link>
    <description>&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Hi All,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;I am trying to automate some code with the use of date&amp;nbsp;macros but encountered some Inavlid date/time/datetime constant errors.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;I have assigned the&amp;nbsp;macros as below&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT face="andale mono,times" size="3"&gt;&lt;CODE class=" language-sas"&gt;%let StartDate ='31oct2015'd;
%put StartDate= &amp;amp;StartDate;
%let Enddate = intnx('month', today(),-1, 'end');
%put EndDate= &amp;amp;EndDate;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;FONT face="andale mono,times" size="3"&gt;&lt;CODE class=" language-sas"&gt;Where CreatedOn&amp;lt;='&amp;amp;Enddate'd and CreatedOn&amp;gt;='&amp;amp;StartDate'd &lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;&amp;nbsp;and using them in the context above.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Any help will be greatly&amp;nbsp;appreciated !&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Adnan&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Mar 2016 15:33:10 GMT</pubDate>
    <dc:creator>Adnan1</dc:creator>
    <dc:date>2016-03-07T15:33:10Z</dc:date>
    <item>
      <title>Date Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254969#M48674</link>
      <description>&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Hi All,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;I am trying to automate some code with the use of date&amp;nbsp;macros but encountered some Inavlid date/time/datetime constant errors.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;I have assigned the&amp;nbsp;macros as below&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT face="andale mono,times" size="3"&gt;&lt;CODE class=" language-sas"&gt;%let StartDate ='31oct2015'd;
%put StartDate= &amp;amp;StartDate;
%let Enddate = intnx('month', today(),-1, 'end');
%put EndDate= &amp;amp;EndDate;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;FONT face="andale mono,times" size="3"&gt;&lt;CODE class=" language-sas"&gt;Where CreatedOn&amp;lt;='&amp;amp;Enddate'd and CreatedOn&amp;gt;='&amp;amp;StartDate'd &lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;&amp;nbsp;and using them in the context above.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Any help will be greatly&amp;nbsp;appreciated !&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="andale mono,times" size="3"&gt;Adnan&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 15:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254969#M48674</guid>
      <dc:creator>Adnan1</dc:creator>
      <dc:date>2016-03-07T15:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: Date Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254973#M48677</link>
      <description>&lt;P&gt;Use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Where CreatedOn&amp;lt;=&amp;amp;Enddate and CreatedOn&amp;gt;=&amp;amp;StartDate
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have a few issues,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First you define Startdate as the string &lt;SPAN class="token datetime number"&gt;'31oct2015'd but then reference as '&amp;amp;startdate'd. Inside single quotes a macro variable does not resolve. Second, since it already has the d at the end you would be attempting to use CreatedOn = ' '31OCT2015'd'd. Recommend pick one stye and stay with it for date literals. I would recommend&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;%let Startdate = 31OCT2015;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;and use as&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;CreatedOn .= "&amp;amp;startdate"d; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;One reason for my preference is I could then use the same variable as part of a title or filename where the quotes and d would be confusing.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;Second. What did you see with the %put for &amp;amp;enddate? You did not create a string that could be used in a date literal. And your actual code for EndDate as shown does not really seem to lend itself to a macro.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token datetime number"&gt;To get EndDate to contain a SAS Date value would have required multiple uses of %sysfunc. You might explain a little further why you need an EndDate macro variable and what the value should look like.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Mar 2016 16:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254973#M48677</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-08T16:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Date Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254982#M48682</link>
      <description>&lt;P&gt;You have a few basic problems in your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First principle to digest:&amp;nbsp; single quotes suppress all macro activity.&amp;nbsp; So this expression does not attempt to resolve &amp;amp;StartDate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CreatedOn &amp;gt;= '&amp;amp;StartDate'd&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's the easy problem to fix.&amp;nbsp; Since StartDate already contains the quotes and the "d", all you need to specify is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CreatedOn &amp;gt;= &amp;amp;StartDate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The more complex problems involve &amp;amp;EndDate.&amp;nbsp; As you can see from your %PUT statement, the %LET statement does not try to execute DATA step functions.&amp;nbsp; INTNX and TODAY() remain characters, just part of the value of &amp;amp;EndDate.&amp;nbsp; The more advanced method to get macro language to execute DATA step functions would be to use the macro language function %SYSFUNC.&amp;nbsp; I'm going to give you a different approach, though, that I think would be more appropriate based on the entire set of issues that I'm seeing here.&amp;nbsp; Add a DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let EndDate= ... same as before ...;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;/P&gt;
&lt;P&gt;end_date = &amp;amp;EndDate;&lt;/P&gt;
&lt;P&gt;call symputx('EndDate', end_date);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That will replace &amp;amp;EndDate with the proper numeric value on SAS's date scale.&amp;nbsp; You could then use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where &amp;amp;StartDate &amp;lt;= CreatedOn &amp;lt;= &amp;amp;EndDate;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where CreatedOn &amp;lt;=&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 16:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254982#M48682</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-03-07T16:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: Date Macros</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254994#M48687</link>
      <description>&lt;P&gt;Hi Adnan,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another&amp;nbsp;similar reply, in slightly different words (written in parallel to the other replies) &amp;nbsp;...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all, you are talking about macro &lt;EM&gt;variables&lt;/EM&gt;. &lt;EM&gt;Macros&lt;/EM&gt; are something else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro variables store text. As you can see from the results of your %PUT statements, macro variables StartDate and EndDate contain exactly what you've entered before. Nothing has been evaluated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro variable references &amp;amp;Enddate and &amp;amp;StartDate in your WHERE statement will not be resolved (i.e. replaced by the content of the respective macro variable) because they are enclosed in single quotes and therefore treated as text. But even if they were resolved (e.g. if you had used double quotes), you would receive error messages because the resulting expressions would look like &lt;FONT face="courier new,courier"&gt;"intnx('month', today(),-1, 'end')"d&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;"'31oct2015'd"d&lt;/FONT&gt;, both of which are invalid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest way to correct your code is to use the values of the macro variables as they are in the WHERE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where CreatedOn&amp;lt;=&amp;amp;Enddate and CreatedOn&amp;gt;=&amp;amp;StartDate;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that the evaluation of TODAY() will then occur when the WHERE statement is executed. If you let %SYSFUNC or a data step evaluate it already in the definition of macro variable Enddate, the results may be different if that definition and the WHERE statement are not executed on the same day.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 16:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Macros/m-p/254994#M48687</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-07T16:42:03Z</dc:date>
    </item>
  </channel>
</rss>

