<?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: Problem with Date Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865626#M341844</link>
    <description>&lt;P&gt;Your WHERE clause is really messed up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all the DATE() function does NOT take any inputs.&amp;nbsp; It just returns today's date.&amp;nbsp; (NOTE you can use the alias TODAY() for it if you want to make that clearer in your code.).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second a date constant must be either the actual number of days since 1960 that SAS uses to store dates, or it must be a quoted string that the DATE informat can recognize immediately followed by the letter D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you meant to use the DATEPART() function?&amp;nbsp; That function does take an input, a datetime value.&amp;nbsp; It will convert the number of seconds since 1960 that SAS uses to represent datetime values into a number of days instead. Basically dividing by the number of seconds in a day.&amp;nbsp; datepart(datetime_value) is equivalent to int(datetime_value/'24:00:00't)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where datepart(Service_date1) between "&amp;amp;report_start"d and "&amp;amp;report_end"d&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Mar 2023 00:01:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-03-22T00:01:42Z</dc:date>
    <item>
      <title>Problem with Date Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865550#M341827</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have set up a date macro to set a range of report period&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let report_start = %sysfunc(intnx(day,%sysfunc(today()),-8,b),date9.); &lt;BR /&gt;%let report_end = %sysfunc(intnx(day,%sysfunc(today()),-2,b),date9.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to use them in my sql query below but getting an Error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Service_Date1 is in datetime format originally.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table Test as &lt;BR /&gt;select * from Table&amp;nbsp;&lt;BR /&gt;where date(Service_date1) &amp;gt;= %bquote(&amp;amp;report_start)&lt;BR /&gt;and date(Service_date1) &amp;lt;= %bquote(&amp;amp;report_end)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 13MAR2023&lt;BR /&gt;-------&lt;BR /&gt;22&lt;BR /&gt;-------&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, -, /, &amp;lt;, &amp;lt;=,&lt;BR /&gt;&amp;lt;&amp;gt;, =, &amp;gt;, &amp;gt;=, AND, EQ, EQT, GE, GET, GROUP, GT, GTT, HAVING, LE, LET, LT, LTT,&lt;BR /&gt;NE, NET, OR, ORDER, ^=, |, ||, ~=.&lt;/P&gt;
&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any thoughts?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 19:05:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865550#M341827</guid>
      <dc:creator>ysk</dc:creator>
      <dc:date>2023-03-21T19:05:21Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Date Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865562#M341828</link>
      <description>&lt;P&gt;Macro variables should not be formatted (unless you are using them in titles, labels or file name in which case a human has to understand them, and then you should format them). For arithmetic and logical operations (which is what you are doing), macro variables should not be formatted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let report_start = %sysfunc(intnx(day,%sysfunc(today()),-8,b));
%let report_end = %sysfunc(intnx(day,%sysfunc(today()),-2,b));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why doesn't it work if it is formatted? Because you have written code that (after the macro variable resolves) is not valid legal working SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where date(Service_date1) &amp;gt;= %bquote(&amp;amp;report_start)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;resolves to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where date(Service_date1) &amp;gt;= 13MAR2023&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and this is not legal SAS code, so you get an error. Do you understand why this is not legal SAS code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, %bquote not needed in this situation.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 19:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865562#M341828</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-03-21T19:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Date Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865586#M341833</link>
      <description>&lt;P&gt;If you leave the macro variable formatted and you are trying to compare it to a SAS date value then you can use a date constant, for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where date(Service_date1) &amp;gt;= "&amp;amp;report_start"d&lt;BR /&gt;and date(Service_date1) &amp;lt;= "&amp;amp;report_end"d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2023 20:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865586#M341833</guid>
      <dc:creator>russt_sas</dc:creator>
      <dc:date>2023-03-21T20:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Date Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865626#M341844</link>
      <description>&lt;P&gt;Your WHERE clause is really messed up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all the DATE() function does NOT take any inputs.&amp;nbsp; It just returns today's date.&amp;nbsp; (NOTE you can use the alias TODAY() for it if you want to make that clearer in your code.).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second a date constant must be either the actual number of days since 1960 that SAS uses to store dates, or it must be a quoted string that the DATE informat can recognize immediately followed by the letter D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you meant to use the DATEPART() function?&amp;nbsp; That function does take an input, a datetime value.&amp;nbsp; It will convert the number of seconds since 1960 that SAS uses to represent datetime values into a number of days instead. Basically dividing by the number of seconds in a day.&amp;nbsp; datepart(datetime_value) is equivalent to int(datetime_value/'24:00:00't)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where datepart(Service_date1) between "&amp;amp;report_start"d and "&amp;amp;report_end"d&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Mar 2023 00:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-Date-Macro/m-p/865626#M341844</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-03-22T00:01:42Z</dc:date>
    </item>
  </channel>
</rss>

