<?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: count the number of visits two hours before a date time stamp in sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622823#M183238</link>
    <description>What have you tried so far?</description>
    <pubDate>Thu, 06 Feb 2020 18:53:39 GMT</pubDate>
    <dc:creator>tomrvincent</dc:creator>
    <dc:date>2020-02-06T18:53:39Z</dc:date>
    <item>
      <title>count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622808#M183234</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to calculate the number of visits happening&amp;nbsp; "two hours"&amp;nbsp;prior to a given sas datetime.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data has 60k records for 1 year worth of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Visit ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Triage_Time&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&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; Oct 1, 2017 00:38&lt;/P&gt;&lt;P&gt;2&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;Oct 1, 2017 00:50&lt;/P&gt;&lt;P&gt;3&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;Oct 1, 2017 01:50&lt;/P&gt;&lt;P&gt;4&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; Oct 1, 2017 01:52&lt;/P&gt;&lt;P&gt;5&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; Oct 1, 2017 03:50&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want my output to be&lt;/P&gt;&lt;P&gt;count(visit ids) if the earliest triage time is two hours prior to the current triage time&lt;/P&gt;&lt;P&gt;So here for the visit 4, the number of visits with triage time 2 hours earlier (earlier than 1:52) will be visit id 1,2,3. (3 visits).&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 18:09:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622808#M183234</guid>
      <dc:creator>jak1351</dc:creator>
      <dc:date>2020-02-06T18:09:45Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622823#M183238</link>
      <description>What have you tried so far?</description>
      <pubDate>Thu, 06 Feb 2020 18:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622823#M183238</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2020-02-06T18:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622842#M183239</link>
      <description>&lt;P&gt;I have tried this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=test;&lt;BR /&gt;by Triage_Completed_DTS;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data temp2;&lt;BR /&gt;set test;&lt;BR /&gt;by Triage_Completed_DTS;&lt;BR /&gt;retain Triage_Completed_DTS;&lt;BR /&gt;if first.Triage_Completed_DTS then ts2=Triage_Completed_DTS;&lt;BR /&gt;hour=(intck('hour',ts2,Triage_Completed_DTS)*120);&lt;BR /&gt;minutes=intck('minute',ts2,Triage_Completed_DTS);&lt;BR /&gt;diff=abs(hour-minutes);&lt;BR /&gt;format TS2 datetime20.;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it gives me 0 hours for the ts2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Triage_completed_DTS is a date time field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried my program using triage date (only date)&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set triage;&lt;BR /&gt;by Triage_Completed_Date;&lt;BR /&gt;if first.Triage_Completed_Date then do;&lt;BR /&gt;n_visits = 1;&lt;BR /&gt;end;&lt;BR /&gt;else n_visits + 1;&lt;BR /&gt;if last.Triage_Completed_Date;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gave me number of visits on that triage date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May be if I could modify the second program and use date time instead of date only and create another variable which gives me the date time of 2 hours prior to this date time and then I can count those visits using the second program.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 19:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622842#M183239</guid>
      <dc:creator>jak1351</dc:creator>
      <dc:date>2020-02-06T19:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622852#M183243</link>
      <description>are you able to retrieve the hour from Triage_Completed_DTS directly?</description>
      <pubDate>Thu, 06 Feb 2020 19:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622852#M183243</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2020-02-06T19:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622853#M183244</link>
      <description>I could but I want it to be done at the date and time interval. Not just the hour.</description>
      <pubDate>Thu, 06 Feb 2020 19:58:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622853#M183244</guid>
      <dc:creator>jak1351</dc:creator>
      <dc:date>2020-02-06T19:58:02Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622858#M183245</link>
      <description>I suggested that because you need to go one step at a time.  Get the hour, get 2 hours earlier, then find the visits.</description>
      <pubDate>Thu, 06 Feb 2020 20:11:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622858#M183245</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2020-02-06T20:11:27Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622883#M183254</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265796"&gt;@jak1351&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a good case for "joining a table with itself". An inner select to join all records in left table with all records from same table as right table where the timestamp is within two hours behind the timestamp in the right table , and an outer select do to the summation and calculate count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* Create input data;
data have;
	input Visit_ID  Triage_Time datetime.;
	format Triage_Time datetime22.;
cards;
1 01Oct2017:00:38
2 01Oct2017:00:50
3 01Oct2017:01:50
4 01Oct2017:01:52
5 01Oct2017:03:50
;
run;

* Calculate number of visits happening two hours prior to current visit;
proc sql;
	create table want as
		select distinct c.Visit_ID, c.Triage_Time, count(*)-1 as visits_within_2_hours
		from (
			select a.Visit_ID, a.Triage_Time
			from have as a left join have as b 
			on b.Triage_Time &amp;lt;= a.Triage_Time and b.Triage_Time &amp;gt;= a.Triage_Time-7200
		) as c
		group by c.Visit_ID
		order by c.Visit_ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="cnt.gif" style="width: 399px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35903i3C4ECFCCB0B29E68/image-size/large?v=v2&amp;amp;px=999" role="button" title="cnt.gif" alt="cnt.gif" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 21:47:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622883#M183254</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-02-06T21:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622925#M183282</link>
      <description>&lt;P&gt;If your data are sorted by triage_time, then you can:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read using the SET statement an observation (whose sequence in the data set is _N_).&lt;/LI&gt;
&lt;LI&gt;In a separate input stream (i.e. a separate SET statement), read (and COUNT) the records until the time difference between the triage_time read in step 1 and the triage_time read in step 2 (renamed to hist_time) is less than or equal to 2 hours.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stated this way, it is a simple task:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input Visit_ID  Triage_Time datetime.;
	format Triage_Time datetime22.;
cards;
1 01Oct2017:00:38
2 01Oct2017:00:50
3 01Oct2017:01:50
4 01Oct2017:01:52
5 01Oct2017:03:50
;

data want (drop=_:);
  set have ;
  do while (triage_time-'02:00:00't &amp;gt; hist_time);
    set have (keep=triage_time rename=triage_time=hist_time);
    _n2+1;
  end;
  count=_n_-(_n2-1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "trick" here is that "_n2+1",&amp;nbsp; a "summing statement" that does nothing more than track the position of the second SET statement as it progress through the data set. &amp;nbsp; Then just compare the "hnistorical" _n2 position to the current position _N_.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited additional note:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "bonus" of the above is that the output dataset have the variable HIST_TIME, which is the first time-stamp within the 2-hour windows.&amp;nbsp; You can use a LAG function as below to get the last time-stamp OUTSIDE the 2-hour window:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have ;
  do while (triage_time-'02:00:00't &amp;gt; hist_time);
    set have (keep=triage_time rename=triage_time=hist_time);
    prior_hist_time=lag(hist_time);
    format prior_hist_time datetime22.;
    retain prior_hist_time;
    _n2+1;
  end;
  count=_n_-(_n2-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2nd edit:&amp;nbsp; I added the RETAIN statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2020 03:13:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/622925#M183282</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-07T03:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of visits two hours before a date time stamp in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/623058#M183342</link>
      <description>&lt;P&gt;Thanks so much. It worked!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2020 14:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-visits-two-hours-before-a-date-time-stamp-in/m-p/623058#M183342</guid>
      <dc:creator>jak1351</dc:creator>
      <dc:date>2020-02-07T14:43:12Z</dc:date>
    </item>
  </channel>
</rss>

