<?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: Merging events into time ranges in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743414#M232753</link>
    <description>&lt;P&gt;Please provide, in the form of working data step, the two starting data sets, and what the resulting data set should look like.&lt;/P&gt;</description>
    <pubDate>Mon, 24 May 2021 18:33:49 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-05-24T18:33:49Z</dc:date>
    <item>
      <title>Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743403#M232744</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;In one data set I have datetime (dt) ranges for hospital days (hday). I another dataset I have datetimes of events. How do I merge the events to hospital days?:&lt;/P&gt;
&lt;P&gt;(hday, start_dt, end_dt) X (event_dt) -&amp;gt; the event happened on which hospital day?&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 17:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743403#M232744</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2021-05-24T17:14:17Z</dc:date>
    </item>
    <item>
      <title>Re: Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743414#M232753</link>
      <description>&lt;P&gt;Please provide, in the form of working data step, the two starting data sets, and what the resulting data set should look like.&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 18:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743414#M232753</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-24T18:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743415#M232754</link>
      <description>&lt;PRE&gt;Here you go:&lt;BR /&gt;&lt;BR /&gt;data d1;
   infile datalines delimiter=' '; 
   input hday start end;
   datalines;                      
1 1 3&lt;BR /&gt;2 4 6&lt;BR /&gt;3 7 9
;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data d2;&lt;BR /&gt;infile datalines delimiter=' '; &lt;BR /&gt;input event; &lt;BR /&gt;2&lt;BR /&gt;3&lt;BR /&gt;5&lt;BR /&gt;8&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;output:&lt;BR /&gt;hday start end event&lt;BR /&gt;1    1   3  2&lt;BR /&gt;1   1    3  3&lt;BR /&gt;2   4    6  5&lt;BR /&gt;3   7    9  8&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 18:48:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743415#M232754</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2021-05-24T18:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743418#M232757</link>
      <description>&lt;P&gt;Thank you for provide sample data (you missed a datalines statement for d2).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code works, and works quickly if your data sets are sorted chronologically and the time ranges in d1 do not overlap:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data d1;
   infile datalines delimiter=' '; 
   input hday start end;
   datalines;                      
1 1 3
2 4 6
3 7 9
;
run;

data d2;
infile datalines delimiter=' '; 
input event; 
datalines;
2
3
5
8
;
run;

data want;
  if 0 then merge d1 d2 ;
  set d1 (keep=start rename=(start=event) in=d1_start)
      d2 (in=event_rec)
      d1 (keep=end rename=(end=event) in=d1_end);
  by event;
  if d1_start then set d1;
  if d1_end then call missing(hday,start,end);
  if event_rec;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a d2 event outside of the d1 ranges, the observation will be recorded with missing hday, start, and end.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "if 0 then merge" statement is there just to produce a desired variable order.&amp;nbsp; Values would be unchanged if you remove it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The key concept here is the "if d1_start then set d1" statement which reads in all the D! variables (unrenamed).&amp;nbsp; They are automatically retained until the next time D!_START=1.&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 18:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743418#M232757</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-24T18:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743419#M232758</link>
      <description>&lt;P&gt;Thank you, this works, but my example was over-simplified. In reality, the start would be something like 2:45 pm, and end - 3:00 pm. I would like to capture an event = 2:55 pm. Is there a way to merge conditionally to hospital day if event is between start and end?:&lt;/P&gt;
&lt;P&gt;hday&amp;nbsp; &amp;nbsp;start&amp;nbsp; end event&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2:45&amp;nbsp; 3:00&amp;nbsp; 2:55&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 19:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743419#M232758</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2021-05-24T19:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Merging events into time ranges</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743421#M232760</link>
      <description>&lt;P&gt;you could use proc sql to do that:&lt;/P&gt;
&lt;PRE&gt;proc sql;&lt;BR /&gt;create table output as&lt;BR /&gt;select d1.hday, d1.start, d1.end, d2.event&lt;BR /&gt;from d2 left join d1 on &lt;EM&gt;&lt;STRONG&gt;d1.start&amp;lt;=d2.event&amp;lt;=d1.end&lt;/STRONG&gt;&lt;/EM&gt;;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 19:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-events-into-time-ranges/m-p/743421#M232760</guid>
      <dc:creator>Angel_Larrion</dc:creator>
      <dc:date>2021-05-24T19:30:03Z</dc:date>
    </item>
  </channel>
</rss>

