<?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: date time conversion to date9. in where clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539468#M148640</link>
    <description>Passing date strings like 01Jan2018 in macro variables can still be used to check a transaction date-time, and your test would also work faster/better without the datepart() function&lt;BR /&gt;e.g.&lt;BR /&gt;t1.txn_reqst_dt BETWEEN "&amp;amp;dateS:0:0:0"dt and "&amp;amp;dateE:23:59:59.99"dt&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Remember to use double quotes around macro vars you need expanded</description>
    <pubDate>Thu, 28 Feb 2019 19:55:22 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2019-02-28T19:55:22Z</dc:date>
    <item>
      <title>date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539439#M148623</link>
      <description>&lt;P&gt;Hello -&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS EG 7.1.&amp;nbsp; I am trying to convert a date time field in my data set to date9. format and can't seem to crack it. My where clause has a condition like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AND t1.txn_reqst_dt BETWEEN &amp;amp;dateS and &amp;amp;dateE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &amp;amp;dateS and &amp;amp;dateE variables have already been converted to date9. format when i declared my global variables, however, the txn_rqst_dt field is a date time stamp field that looks like this&amp;nbsp;04MAY2018:13:15:32.238000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the error i get:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;76 AND datepart(t1.txn_reqst_dt) BETWEEN &amp;amp;dateS and &amp;amp;dateE&lt;BR /&gt;NOTE: Line generated by the macro variable "DATES".&lt;BR /&gt;76 01Jan2018&lt;BR /&gt;_______&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: !!, *, **, +, -, /, AND, ||.&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 suggestions?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 18:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539439#M148623</guid>
      <dc:creator>CJM8</dc:creator>
      <dc:date>2019-02-28T18:44:13Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539453#M148630</link>
      <description>&lt;P&gt;This is not a date in SAS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;01Jan2018&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's just a bunch of characters.&amp;nbsp; If you want to refer to a date, you would need to use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"01Jan2018"d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in your program that would translate to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BETWEEN "&amp;amp;dateS"d and "&amp;amp;dateE"d&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539453#M148630</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-28T19:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539455#M148632</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23156"&gt;@CJM8&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to convert a date time field in my data set to date9. format and can't seem to crack it. My where clause has a condition like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AND t1.txn_reqst_dt BETWEEN &amp;amp;dateS and &amp;amp;dateE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &amp;amp;dateS and &amp;amp;dateE variables have already been converted to date9. format when i declared my global variables&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Formatting your macro variables to have a date9. format (or any other date or datetime or time format) for use in a where clause, or for use in any comparison, is usually a mistake. Leave them as un-formatted SAS date (or datetime or time) values.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539455#M148632</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-28T19:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539460#M148634</link>
      <description>&lt;P&gt;Thank you for your replies. I removed the global variables, since they weren't really necessary for this anyway...and replaced with a prompt. However, i still get the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;78 AND datepart(t1.txn_reqst_dt) BETWEEN '&amp;amp;dateS'd and '&amp;amp;dateE'd&lt;BR /&gt;ERROR: Invalid date/time/datetime constant '&amp;amp;dateS'd.&lt;BR /&gt;ERROR: Invalid date/time/datetime constant '&amp;amp;dateE'd.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any other suggestions?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:38:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539460#M148634</guid>
      <dc:creator>CJM8</dc:creator>
      <dc:date>2019-02-28T19:38:55Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539461#M148635</link>
      <description>&lt;P&gt;See Maxim 28.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539461#M148635</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-28T19:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539465#M148637</link>
      <description>&lt;P&gt;You need double quotes, not single quotes, as stated above by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or just get rid of the formatting and get rid of the quotes.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539465#M148637</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-28T19:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539466#M148638</link>
      <description>You didn't use my suggestion.  Change the single quotes to double quotes.  Single quotes suppress all macro activity including resolution of macro variables.</description>
      <pubDate>Thu, 28 Feb 2019 19:48:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539466#M148638</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-02-28T19:48:09Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539467#M148639</link>
      <description>&lt;P&gt;thank you everyone - this ran correctly!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;AND datepart(t1.txn_reqst_dt) BETWEEN "&amp;amp;dateS"d and "&amp;amp;dateE"d&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am going to make a copy of the 28 maxims and send to my team if you don't mind!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks again&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 19:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539467#M148639</guid>
      <dc:creator>CJM8</dc:creator>
      <dc:date>2019-02-28T19:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539468#M148640</link>
      <description>Passing date strings like 01Jan2018 in macro variables can still be used to check a transaction date-time, and your test would also work faster/better without the datepart() function&lt;BR /&gt;e.g.&lt;BR /&gt;t1.txn_reqst_dt BETWEEN "&amp;amp;dateS:0:0:0"dt and "&amp;amp;dateE:23:59:59.99"dt&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Remember to use double quotes around macro vars you need expanded</description>
      <pubDate>Thu, 28 Feb 2019 19:55:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539468#M148640</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2019-02-28T19:55:22Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539485#M148652</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15174"&gt;@Peter_C&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;your test would also work faster/better without the datepart() function&lt;BR /&gt;e.g.&lt;BR /&gt;t1.txn_reqst_dt BETWEEN "&amp;amp;dateS:0:0:0"dt and "&amp;amp;dateE:23:59:59.99"dt&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wouldn't it work even faster/better without converting date/time values to formatted versions, and then having to "unformat" the date/time values in the WHERE clause via "&amp;amp;dateS:0:0:0"dt?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Feb 2019 21:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539485#M148652</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-28T21:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539544#M148680</link>
      <description>Compile time of vanishingly small order can be compared with runtime execution of the datepart() function&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;It really is a vanishingly small amount of time to compile a date or datetime constant.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Reading SAS logs to decypher a problem is far easier to validate when a macro var resolves to a date string rather than a number of days or seconds.&lt;BR /&gt;&lt;BR /&gt;That is why I always recommend using a date string and date constants, rather than a "number of days"&amp;nbsp; &amp;nbsp;to represent a date in coding&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;(Use a very different approach for data)&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 01 Mar 2019 06:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539544#M148680</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2019-03-01T06:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: date time conversion to date9. in where clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539548#M148683</link>
      <description>&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15174"&gt;@Peter_C&lt;/a&gt;&amp;nbsp;.&amp;nbsp; I prefer using date and datetime literals, as the log messages are clearer.&amp;nbsp; The one-off compile time conversion of the date or datetime literal to the equivalent internal value would be minuscule.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   do dt="01JAN2018:00:00:00"dt to "31DEC2018:23:59:59"dt;
      output;
   end;
   format dt datetime.;
run;

data _null_;
   start="01APR2018:00:00:00"dt;
   end="30APR2018:23:59:59"dt;
   call symputx("start1",start);
   call symputx("start2",put(start,datetime.));
   call symputx("end1",  end);
   call symputx("end2",  put(end,datetime.));
run;
%put &amp;amp;=start1 &amp;amp;=start2;
%put &amp;amp;=end1 &amp;amp;end2;

data want1;
   set have;
   where dt between &amp;amp;start1 and &amp;amp;end1;
run;

data want2;
   set have;
   where dt between "&amp;amp;start2"dt and "&amp;amp;end2"dt;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I like the log messages in want2 better.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 07:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/date-time-conversion-to-date9-in-where-clause/m-p/539548#M148683</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-03-01T07:16:33Z</dc:date>
    </item>
  </channel>
</rss>

