<?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: Create Count Based on Dates &amp;amp; Custom Date Logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613124#M179026</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Good question - so the way these cases would be handled is, you go 60 mins from the FIRST event. So in your example it would count as 2 events (the first two get grouped into one, and since the last one is greater than 60 mins from the first, it would be a separate event.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also note that: if an event falls on a separate day but is within 60 mins of the first event (lets say the events were 01OCT2019:11:30:12 and 02OCT2019:00:10:10) it counts as two separate events.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Dec 2019 18:38:44 GMT</pubDate>
    <dc:creator>Time_Looper47</dc:creator>
    <dc:date>2019-12-19T18:38:44Z</dc:date>
    <item>
      <title>Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612757#M178858</link>
      <description>&lt;P&gt;Greetings,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So what I'm looking to do is create a count as a number of events&amp;nbsp;for each key_id based on unique dates (with a caveat). My data looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;key_id | event_dt | event_ts&lt;/P&gt;&lt;P&gt;1142 | 10JUL2019 | 10JUL2019:18:45:12&lt;/P&gt;&lt;P&gt;1142 | 10JUL2019 | 10JUL2019:18:00:39&lt;/P&gt;&lt;P&gt;1142 | 09JUL2019 | 09JUL2019:16:32:51&lt;/P&gt;&lt;P&gt;1142 | 15JUL2019 | 15JUL2019:12:30:34&lt;/P&gt;&lt;P&gt;3462 | 01OCT2019 | 01OCT2019:14:29:43&lt;/P&gt;&lt;P&gt;3462 | 01OCT2019 | 01OCT2019:14:00:12&lt;/P&gt;&lt;P&gt;3462 | 01OCT2019 | 01OCT2019:14:45:01&lt;/P&gt;&lt;P&gt;3462 | 12NOV2019 | 12NOV2019:11:17:21&lt;/P&gt;&lt;P&gt;4444 | 04AUG2019 | 04AUG2019:11:20:35&lt;/P&gt;&lt;P&gt;4444 | 02MAR2019 | 02MAR2019:10:55:13&lt;/P&gt;&lt;P&gt;4444 | 14DEC2019 | 14DEC2019:14:32:17&lt;/P&gt;&lt;P&gt;4444 | 21OCT2019 | 21OCT2019:14:12:42&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;event_dt format is: DATE9.&lt;/P&gt;&lt;P&gt;event_ts format is: DATETIME19.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The caveat is that, I want to create a count (by key_id)&amp;nbsp;of events with&amp;nbsp;unique dates, however, if an event falls on the same day and is within 60 mins of the other, I don't want to count that as a second event. So, for the above data, I would&amp;nbsp;desire something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;key_id | count_event&lt;/P&gt;&lt;P&gt;1142 | 3&lt;/P&gt;&lt;P&gt;3462 | 2&lt;/P&gt;&lt;P&gt;4444 | 4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 18:23:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612757#M178858</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-12-18T18:23:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612762#M178860</link>
      <description>&lt;P&gt;Questions: Does your 60- minute filter cross date boundaries,&amp;nbsp; i.e. is 11JUL2019:00:15:00 within the same event as 10JUL2019:23:45:00?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If yes, then&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm='|';
  input key_id  event_dt :date9.   event_ts  datetime18. ;
  format event_dt date9.  event_ts datetime18.;
datalines;
1142 | 10JUL2019 | 10JUL2019:18:45:12
1142 | 10JUL2019 | 10JUL2019:18:00:39
1142 | 09JUL2019 | 09JUL2019:16:32:51
1142 | 15JUL2019 | 15JUL2019:12:30:34
3462 | 01OCT2019 | 01OCT2019:14:29:43
3462 | 01OCT2019 | 01OCT2019:14:00:12
3462 | 01OCT2019 | 01OCT2019:14:45:01
3462 | 12NOV2019 | 12NOV2019:11:17:21
4444 | 04AUG2019 | 04AUG2019:11:20:35
4444 | 02MAR2019 | 02MAR2019:10:55:13
4444 | 14DEC2019 | 14DEC2019:14:32:17
4444 | 21OCT2019 | 21OCT2019:14:12:42
run;

proc sort data=have;
   by key_id event_ts;
run;

data want (keep=key_id n_events;
  set have;
  by key_id;

  if event_ts &amp;gt; intnx('minute',lag(event_ts),60) then n_events+1;
  if first.key_id then n_events=1;
  if last.key_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if your time stamps were to the second, rather than to the whole minute, you could have an error for the time span from, say, 13:00:32 to 14:00:58 which is more the 60 minutes (by an exce3ss of 36 seconds), but would not be detected because the INTNX function is using a granularity of 'minute'.&amp;nbsp; You could address this via:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if event_ts &amp;gt; intnx('second',lag(event_ts),3600) then n_events+1;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 18:56:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612762#M178860</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-12-18T18:56:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612828#M178902</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293046"&gt;@Time_Looper47&lt;/a&gt;&amp;nbsp; Go with genie mark's solution. I am bored, so my share of fun&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input key_id  @8 event_dt date9. @20 event_ts datetime20.;
format event_dt date9. event_ts datetime20.;
cards;
1142 | 10JUL2019 | 10JUL2019:18:45:12
1142 | 10JUL2019 | 10JUL2019:18:00:39
1142 | 09JUL2019 | 09JUL2019:16:32:51
1142 | 15JUL2019 | 15JUL2019:12:30:34
3462 | 01OCT2019 | 01OCT2019:14:29:43
3462 | 01OCT2019 | 01OCT2019:14:00:12
3462 | 01OCT2019 | 01OCT2019:14:45:01
3462 | 12NOV2019 | 12NOV2019:11:17:21
4444 | 04AUG2019 | 04AUG2019:11:20:35
4444 | 02MAR2019 | 02MAR2019:10:55:13
4444 | 14DEC2019 | 14DEC2019:14:32:17
4444 | 21OCT2019 | 21OCT2019:14:12:42
;

data want;
 do until(last.key_id);
  do until(last.event_dt);
   set have;
   by key_id event_dt notsorted;
   if not first.event_dt and abs(dif(minute(event_ts)))&amp;lt;=60 then continue;
   count_event=sum(count_event,1);
  end;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 18 Dec 2019 20:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612828#M178902</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-18T20:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612833#M178905</link>
      <description>&lt;P&gt;Hey &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;, good question - it does NOT actually cross boundaries. If it's a new day (but within 60 minutes from a previous day)&amp;nbsp;it's a new event no matter what&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 20:40:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612833#M178905</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-12-18T20:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612848#M178914</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I modify this to address the fact that no matter what if it's a new day, it's a new event, even if it's within 60 mins across the boundary?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 21:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612848#M178914</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-12-18T21:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612881#M178923</link>
      <description>&lt;P&gt;What if you have dates&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;01OCT2019:14:00:12&lt;BR /&gt;01OCT2019:14:15:12&lt;BR /&gt;01OCT2019:15:10:12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first two are within 60 mins, so are the last two, but not the first and last. Does that count as one or two distinct events? What's the rule for such cases?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2019 23:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/612881#M178923</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-12-18T23:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613124#M179026</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Good question - so the way these cases would be handled is, you go 60 mins from the FIRST event. So in your example it would count as 2 events (the first two get grouped into one, and since the last one is greater than 60 mins from the first, it would be a separate event.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also note that: if an event falls on a separate day but is within 60 mins of the first event (lets say the events were 01OCT2019:11:30:12 and 02OCT2019:00:10:10) it counts as two separate events.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 18:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613124#M179026</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-12-19T18:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613132#M179030</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293046"&gt;@Time_Looper47&lt;/a&gt;&amp;nbsp; Can you try this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=_have;
by key_id event_dt event_ts;
run;
data want;
 set _have;
 by key_id event_dt ;
 retain t;
 if first.key_id  then do; count=1;t=event_ts;end;
 else if first.event_dt then do; count+1;t=event_ts;end;
 else if intck('minute',t,event_ts)&amp;gt;60 then do;
  t=event_ts;
  count+1;
 end;
 if last.key_id;
 keep key_id count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Dec 2019 19:25:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613132#M179030</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-19T19:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Create Count Based on Dates &amp; Custom Date Logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613139#M179032</link>
      <description>&lt;P&gt;This would work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines dlm='|';
  input key_id  event_dt :date9.   event_ts  datetime18. ;
  format event_dt date9.  event_ts datetime18.;
datalines;
1142 | 10JUL2019 | 10JUL2019:18:45:12
1142 | 10JUL2019 | 10JUL2019:18:00:39
1142 | 09JUL2019 | 09JUL2019:16:32:51
1142 | 15JUL2019 | 15JUL2019:12:30:34
3462 | 01OCT2019 | 01OCT2019:14:29:43
3462 | 01OCT2019 | 01OCT2019:14:00:12
3462 | 01OCT2019 | 01OCT2019:14:45:01
3462 | 12NOV2019 | 12NOV2019:11:17:21
4444 | 04AUG2019 | 04AUG2019:11:20:35
4444 | 02MAR2019 | 02MAR2019:10:55:13
4444 | 14DEC2019 | 14DEC2019:14:32:17
4444 | 21OCT2019 | 21OCT2019:14:12:42
4444 | 21OCT2019 | 21OCT2019:23:12:42  
4444 | 22OCT2019 | 22OCT2019:00:02:42  New day
4444 | 22OCT2019 | 22OCT2019:00:52:42  Same event
4444 | 22OCT2019 | 22OCT2019:01:12:42  New event
run;

proc sort data=have;
   by key_id event_ts;
run;

data temp (keep=key_id event);
retain first_event_ts;
set have; by key_id;
if 	first.key_id or 
	event_dt &amp;gt; lag(event_dt) or 
	intnx("minute", event_ts, -60) &amp;gt; first_event_ts then do;
		event + 1;
		first_event_ts = event_ts;
		end;	
run;

proc sql;
create table want as
select 
	key_id ,
	count (distinct event) as count_event
from temp
group by key_id;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note: I added data to test your latest requirements.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2019 20:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Count-Based-on-Dates-amp-Custom-Date-Logic/m-p/613139#M179032</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-12-19T20:06:07Z</dc:date>
    </item>
  </channel>
</rss>

