<?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: Query to extract datetimes in one table between datetimes found in a second table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259193#M50099</link>
    <description>&lt;P&gt;Sounds like time to provide example&amp;nbsp;input&amp;nbsp;data with all of the variables (dummy is fine as long as it replicates the problem) and what the actual output desired for that example data may be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, One other point: Those values are SAS Datetime numeric values aren't they and not character?&lt;/P&gt;</description>
    <pubDate>Fri, 25 Mar 2016 23:06:27 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-03-25T23:06:27Z</dc:date>
    <item>
      <title>Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259119#M50067</link>
      <description>&lt;P&gt;Hello community! Hopefully someone can help, as I am stuck&lt;/P&gt;&lt;P&gt;I have data in table 1 with variables and values with datetimes with each row corresponding to 1/40th of a second, with over 30 days per ID. I have data in table 2 that has the start and stop datetimes for each date for each ID.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I want SQL to pull out data in table 1 between the datetimes for each day in table 2.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;Table 1&lt;/P&gt;&lt;P&gt;datetimestamp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id&lt;/P&gt;&lt;P&gt;21MAY15:06:14:57.250&amp;nbsp; se10&lt;BR /&gt;21MAY15:06:14:57.275&amp;nbsp; se10&lt;BR /&gt;21MAY15:06:14:57.300&amp;nbsp; se10&lt;BR /&gt;21MAY15:06:14:57.325&amp;nbsp; se10&lt;BR /&gt;21MAY15:06:14:57.350&amp;nbsp; se10&lt;BR /&gt;21MAY15:06:14:57.375&amp;nbsp; se10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 2&lt;/P&gt;&lt;P&gt;beddatetime&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wakedatetime&lt;/P&gt;&lt;P&gt;20MAY15:21:30:00.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;21MAY15:06:15:00.0000&lt;BR /&gt;20MAY15:21:30:00.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;21MAY15:06:15:00.0000&lt;BR /&gt;20MAY15:21:30:00.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;21MAY15:06:15:00.0000&lt;BR /&gt;20MAY15:21:30:00.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;21MAY15:06:15:00.0000&lt;BR /&gt;21MAY15:21:42:40.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;22MAY15:07:23:00.0000&lt;BR /&gt;21MAY15:21:42:40.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;22MAY15:07:23:00.0000&lt;BR /&gt;21MAY15:21:42:40.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;22MAY15:07:23:00.0000&lt;BR /&gt;21MAY15:21:42:40.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;22MAY15:07:23:00.0000&lt;BR /&gt;21MAY15:21:42:40.0000&amp;nbsp;&amp;nbsp; &amp;nbsp;22MAY15:07:23:00.0000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, the below code pulls out the correct datetimestamp only if beddatetime is after midnight. If beddatetime is prior to midnight then it just stops at midnight and the information until midnight is lost.&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;CREATE TABLE&amp;nbsp;&lt;STRONG&gt;want&lt;/STRONG&gt; AS&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;SELECT&amp;nbsp;&amp;nbsp; &amp;nbsp;t2.x, t2.y, t2.z, t2.lux, t2.event, t2.temp, &lt;STRONG&gt;t2.id&lt;/STRONG&gt;, /*t2.date, t2.timestamp,*/&lt;STRONG&gt; t2.datetimestamp&lt;/STRONG&gt;,&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;STRONG&gt;t3.id AS id1,&lt;/STRONG&gt; /*t3.date AS date1, t3.waketime, t3.bedtime,*/ t3.day, &lt;STRONG&gt;t3.beddatetime, t3.wakedatetime,&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;t3.sleep_h&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;FROM WORK.DATA_RAW1 t2&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;NATURAL JOIN surveydata t3&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;WHERE t2.datetimestamp BETWEEN t3.beddatetime AND t3.wakedatetime&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;ORDER by id, datetimestamp ;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 18:56:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259119#M50067</guid>
      <dc:creator>jkurka</dc:creator>
      <dc:date>2016-03-25T18:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259125#M50070</link>
      <description>&lt;P&gt;Do not use NATURAL JOIN for this, as it will enforce t2.date=t3.date.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 19:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259125#M50070</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-25T19:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259135#M50074</link>
      <description>&lt;P&gt;Even though they have been coded out? With only the ID, datetimestamp, wakedatetimestamp, and beddatetimestamp, the output is still incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any time I have called for an inner join my computer freezes up after 10 or so minutes.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 19:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259135#M50074</guid>
      <dc:creator>jkurka</dc:creator>
      <dc:date>2016-03-25T19:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259143#M50078</link>
      <description>&lt;P&gt;If T3 is your constraint table you might try a RIGHT JOIN instead of NATURAL JOIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 20:06:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259143#M50078</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-25T20:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259155#M50085</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Natural join&lt;/STRONG&gt; joins on all common named variables whether you mention them in your select list or not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Build smaller datasets for testing. Get the correct result on those before scaling up to your full datasets.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 21:01:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259155#M50085</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-25T21:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259192#M50098</link>
      <description>&lt;P&gt;The constraining table has other IDs with respective wakedatetimes and beddatetimes also, and is producing some odd results that still are cutting off the data at midnight.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 23:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259192#M50098</guid>
      <dc:creator>jkurka</dc:creator>
      <dc:date>2016-03-25T23:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259193#M50099</link>
      <description>&lt;P&gt;Sounds like time to provide example&amp;nbsp;input&amp;nbsp;data with all of the variables (dummy is fine as long as it replicates the problem) and what the actual output desired for that example data may be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, One other point: Those values are SAS Datetime numeric values aren't they and not character?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2016 23:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259193#M50099</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-25T23:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: Query to extract datetimes in one table between datetimes found in a second table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259197#M50101</link>
      <description>&lt;P&gt;It's solved! Using the NATURAL JOIN was the only join that worked, but I removed date and instead used a generic number to keep the data set in order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sometimes just hashing these things out with others gets the brain flowing!&lt;/P&gt;</description>
      <pubDate>Sat, 26 Mar 2016 00:36:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Query-to-extract-datetimes-in-one-table-between-datetimes-found/m-p/259197#M50101</guid>
      <dc:creator>jkurka</dc:creator>
      <dc:date>2016-03-26T00:36:24Z</dc:date>
    </item>
  </channel>
</rss>

