<?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 Where Statement Syntax in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891384#M352144</link>
    <description>&lt;P&gt;I should have included this in my original question, but I don't care about the time portion of it.&amp;nbsp; I would prefer to just use the date portion in my where statement and capture all the various times within that date.&amp;nbsp; Is that possible?&lt;/P&gt;</description>
    <pubDate>Mon, 28 Aug 2023 19:07:32 GMT</pubDate>
    <dc:creator>Cheesiepoof05</dc:creator>
    <dc:date>2023-08-28T19:07:32Z</dc:date>
    <item>
      <title>Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891378#M352140</link>
      <description>&lt;DIV&gt;I'm trying to limit some results to within a certain date range and struggling with the syntax of how the date should be entered in my WHERE statement.&amp;nbsp; The field is "datetime" format at the source database with numeric precision of 23.&amp;nbsp; Looks like this "2022-08-05 12:35:08.637".&amp;nbsp; Any ideas with how the date should be entered in my where statement?&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;PROC SQL;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp;CREATE TABLE WORK.Results AS&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp;SELECT&lt;/DIV&gt;
&lt;DIV&gt;Fruit,&lt;/DIV&gt;
&lt;DIV&gt;Date&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; FROM Database.Table&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; WHERE&amp;nbsp; Date &amp;gt;= 2022-08-05 12:35:08.637 and &amp;lt;= 2022-08-06 12:35:08.637&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;;&lt;/DIV&gt;
&lt;DIV&gt;QUIT;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 28 Aug 2023 18:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891378#M352140</guid>
      <dc:creator>Cheesiepoof05</dc:creator>
      <dc:date>2023-08-28T18:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891380#M352141</link>
      <description>&lt;P&gt;No idea what a numerical precision of 23 means with a datetime value.&lt;/P&gt;
&lt;P&gt;Are those milliseconds in your example time points really significant?&amp;nbsp; That seems a little bit too much to ask for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To enter a datetime constant it has to be something the DATETIME informat can interpret.&amp;nbsp; You then enclose it in quotes and adds the letters DT after the closing quote.&amp;nbsp; That is a datetime literal in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your WHERE clause also has to use valid syntax.&amp;nbsp; You cannot have the AND operator next to the LESS THAN OR EQUAL TO operator.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you just want to use the BETWEEN syntax.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE Date between "05AUG2022:12:35:08.637"dt and "06AUG2022:12:35:08.637"dt&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note you can also use BETWEEN in a normal WHERE statements outside of PROC SQL.&amp;nbsp; That way you can skip trying use PROC SQL and just use normal SAS code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Results;
  set Database.Table(Keep=fruit date);
  WHERE Date between "05AUG2022:12:35:08.637"dt and "06AUG2022:12:35:08.637"dt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Aug 2023 18:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891380#M352141</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-28T18:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891381#M352142</link>
      <description>&lt;P&gt;Apparently your DATE variable takes datetime values (i.e. number of seconds after Midnight at the start of Jan 1, 1960).&amp;nbsp; So if you want to express a constant for that variable you have to use the datetime-literal format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I.e. your where clause should be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  where date &amp;gt;= '05aug2022:12:35:08.637'dt
  and
        date &amp;lt;= '06aug2022:12:35:08.637'dt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 28 Aug 2023 18:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891381#M352142</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-28T18:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891384#M352144</link>
      <description>&lt;P&gt;I should have included this in my original question, but I don't care about the time portion of it.&amp;nbsp; I would prefer to just use the date portion in my where statement and capture all the various times within that date.&amp;nbsp; Is that possible?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Aug 2023 19:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891384#M352144</guid>
      <dc:creator>Cheesiepoof05</dc:creator>
      <dc:date>2023-08-28T19:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891386#M352146</link>
      <description>&lt;P&gt;If variable named DATE is actually a SAS date value, and not (as we assumed) a SAS date/time value, then yes of course you can do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But they key question is about whether or not the variable named DATE is a date value or a date/time value. Which is it?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Aug 2023 19:11:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891386#M352146</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-28T19:11:00Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891388#M352148</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/434710"&gt;@Cheesiepoof05&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I should have included this in my original question, but I don't care about the time portion of it.&amp;nbsp; I would prefer to just use the date portion in my where statement and capture all the various times within that date.&amp;nbsp; Is that possible?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case you could use the DATEPART() function and date literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where datepart(date) between '05AUG2022'd and "06AUG2022"d&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if the source is actually some external database then for performance reasons you might need to still need to use datettime values.&amp;nbsp; Otherwise SAS might try to pull the whole database over to SAS so it can use the DATEPART() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where '05AUG2022:00:00'dt &amp;lt;= date &amp;lt; "07AUG2022:00:00"dt&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice how I used midnight on the next day for the upper bound and use &amp;lt; instead of &amp;lt;= for the comparison test.&amp;nbsp; That will make sure that timestamps that are milliseconds before midnight are included.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Aug 2023 19:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891388#M352148</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-28T19:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: Date Where Statement Syntax</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891392#M352151</link>
      <description>&lt;P&gt;It is a date/time value, not just a date value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried both methodologies and timed the return time.&amp;nbsp; They both worked and returned the same results but the first methodology returned in half the time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;1 - where DATE between '05JAN2023 00:00:00'dt and '05MAY2023 23:59:59'dt&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;2 - where datepart(DATE) between '05JAN2023'd and '05MAY2023'd&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;I appreciate all of the help everyone has provided!&amp;nbsp; As I'm still very new to SAS (and obviously using more SQL rather than SAS), anyone have a recommendation on a good learning course for beginners to build up my skillset?&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 28 Aug 2023 19:28:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-Where-Statement-Syntax/m-p/891392#M352151</guid>
      <dc:creator>Cheesiepoof05</dc:creator>
      <dc:date>2023-08-28T19:28:50Z</dc:date>
    </item>
  </channel>
</rss>

