<?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: Comparing datepart(datetime) to date in proc sql in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939499#M42201</link>
    <description>&lt;P&gt;It should be the calculated version of PDDATE (i.e. the date, not datetime).&amp;nbsp; Without the calculated keyword, the original (i.e. the datetime) is used, correct?&amp;nbsp; So why doesn't comparing it to a date constant result in an error?&lt;/P&gt;</description>
    <pubDate>Thu, 15 Aug 2024 19:20:14 GMT</pubDate>
    <dc:creator>osbornejo</dc:creator>
    <dc:date>2024-08-15T19:20:14Z</dc:date>
    <item>
      <title>Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939480#M42197</link>
      <description>&lt;P&gt;I've run into a situation where I'm not sure why some code is working:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll start by saying that pddate is in datetime20 format in the source:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="osbornejo_0-1723740929800.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99314i9F0FBFF0DDB8575D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="osbornejo_0-1723740929800.png" alt="osbornejo_0-1723740929800.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should also note that this is in SAS EG.&amp;nbsp; Here is the query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;PROC SQL;
&amp;nbsp; &amp;nbsp; CREATE TABLE WANT AS
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT DATEPART(PDDATE) AS PDDATE FORMAT DATE9., [more variables that aren't relevant]
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM SOURCE
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE PDDATE BETWEEN '01JAN2024'd AND '31JAN2024'd;
QUIT;&lt;/LI-CODE&gt;
&lt;P&gt;(I realize that naming the calculated variable the same as the original probably isn't the best practice, but please bear with me.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding is that if you are calculating a new variable in the SELECT statement, you can only filter by that variable in the HAVING statement, not the WHERE statement.&amp;nbsp; So if I want PDDATE to be a date and not a datetime, I need to use it in a HAVING statement - it has not yet been converted to a date in the WHERE statement.&amp;nbsp; Therefore, I would expect comparing PDDATE to a date (instead of a datetime) in the WHERE statement to result in an error.&amp;nbsp; But it doesn't; I get the desired output, and I'm not sure why.&amp;nbsp; Is my understanding of the interaction between a calculated variable and WHERE/HAVING incorrect?&amp;nbsp; Is SAS EG fixing it behind the scenes?&amp;nbsp; What am I missing here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unrelated question:&amp;nbsp; What is the difference between labels and message tags when posting a question?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 17:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939480#M42197</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2024-08-15T17:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939482#M42198</link>
      <description>&lt;P&gt;Which version of PDDATE did you want the WHERE clause to use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want be sure the WHERE clause will use the version of PDDATE that your SELECT list created then add the CALCULATED keyword.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE calculated PDDATE BETWEEN '01JAN2024'd AND '31JAN2024'd&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to be sure that the WHERE clause will use the original PDDATE then include the ALIAS.&amp;nbsp; Since the original PDDATE is datetime you will want to include the DATEPART() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE datepart(SOURCE.PDDATE) BETWEEN '01JAN2024'd AND '31JAN2024'd&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps use DATETIME constants instead.&amp;nbsp; If the values of PDDATE include non-zero time of day parts then be careful.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;WHERE '01JAN2024:00:00'dt &amp;lt;= SOURCE.PDDATE &amp;lt; '01FEB2024:00:00'dt&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 17:28:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939482#M42198</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-15T17:28:08Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939484#M42199</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406934"&gt;@osbornejo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp; Is my understanding of the interaction between a calculated variable and WHERE/HAVING incorrect?&amp;nbsp; Is SAS EG fixing it behind the scenes?&amp;nbsp; What am I missing here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You're missing the fact that it runs, but not correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both are numeric variables at the root and the comparison is run but you will not get correct data back. SAS stores dates/datetimes as numbers but the ranges are different. Dates are the the number of days from Jan 1, 1960 and datetimes is the number of seconds from Jan 1, 1960.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the where range is valid when interpreted as a set of numbers but isn't correct for the comparison you want to make.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;(I realize that naming the calculated variable the same as the original probably isn't the best practice, but please bear with me.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is an example where this is important because it makes it more difficult to trace the root issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As indicated by others, use the CALCULATED keyword to use the new variable in the WHERE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406934"&gt;@osbornejo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I've run into a situation where I'm not sure why some code is working:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll start by saying that pddate is in datetime20 format in the source:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="osbornejo_0-1723740929800.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/99314i9F0FBFF0DDB8575D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="osbornejo_0-1723740929800.png" alt="osbornejo_0-1723740929800.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I should also note that this is in SAS EG.&amp;nbsp; Here is the query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;PROC SQL;
&amp;nbsp; &amp;nbsp; CREATE TABLE WANT AS
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SELECT DATEPART(PDDATE) AS PDDATE FORMAT DATE9., [more variables that aren't relevant]
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FROM SOURCE
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHERE PDDATE BETWEEN '01JAN2024'd AND '31JAN2024'd;
QUIT;&lt;/LI-CODE&gt;
&lt;P&gt;(I realize that naming the calculated variable the same as the original probably isn't the best practice, but please bear with me.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding is that if you are calculating a new variable in the SELECT statement, you can only filter by that variable in the HAVING statement, not the WHERE statement.&amp;nbsp; So if I want PDDATE to be a date and not a datetime, I need to use it in a HAVING statement - it has not yet been converted to a date in the WHERE statement.&amp;nbsp; Therefore, I would expect comparing PDDATE to a date (instead of a datetime) in the WHERE statement to result in an error.&amp;nbsp; But it doesn't; I get the desired output, and I'm not sure why.&amp;nbsp; Is my understanding of the interaction between a calculated variable and WHERE/HAVING incorrect?&amp;nbsp; Is SAS EG fixing it behind the scenes?&amp;nbsp; What am I missing here?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unrelated question:&amp;nbsp; What is the difference between labels and message tags when posting a question?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 17:52:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939484#M42199</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-08-15T17:52:21Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939499#M42201</link>
      <description>&lt;P&gt;It should be the calculated version of PDDATE (i.e. the date, not datetime).&amp;nbsp; Without the calculated keyword, the original (i.e. the datetime) is used, correct?&amp;nbsp; So why doesn't comparing it to a date constant result in an error?&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 19:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939499#M42201</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2024-08-15T19:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939500#M42202</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;You're missing the fact that it runs, but not correctly.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;That's the thing - the results have dates that are in the desired range.&amp;nbsp; I agree that the comparison shouldn't be working, which is why I'm so surprised by the results.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 19:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939500#M42202</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2024-08-15T19:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939517#M42203</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406934"&gt;@osbornejo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It should be the calculated version of PDDATE (i.e. the date, not datetime).&amp;nbsp; Without the calculated keyword, the original (i.e. the datetime) is used, correct?&amp;nbsp; So why doesn't comparing it to a date constant result in an error?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because both dates and datetimes are stored as numbers, so the comparison is syntactically valid. That the numbers are in vastly different ranges doesn't bother the SQL procedure.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Aug 2024 21:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939517#M42203</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-08-15T21:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing datepart(datetime) to date in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939707#M42211</link>
      <description>&lt;P&gt;That's interesting. In a mockup test created that did NOT occur. Can you confirm the format of the original variable?&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>Fri, 16 Aug 2024 21:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Comparing-datepart-datetime-to-date-in-proc-sql/m-p/939707#M42211</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-08-16T21:53:55Z</dc:date>
    </item>
  </channel>
</rss>

