<?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 distinct hours in past 3hr in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418287#M102746</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;This is still several orders of magnitude slower than SQL.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Dec 2017 20:23:45 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2017-12-04T20:23:45Z</dc:date>
    <item>
      <title>Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417901#M102650</link>
      <description>&lt;P&gt;Hi everyone, I have one more problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't get the count of unique(distinct) values to use in hash table calculation.&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;  opt  &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt;   t_purchase   : datetime32&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; t_spent :datetime32&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;  usage &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;format&lt;/SPAN&gt;  t_purchase  t_spent datetime32&lt;SPAN class="token punctuation"&gt;. &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00    3  
1    a1  10NOV2017:12:02:00  10NOV2017:15:20:33    0 
1    a1  10NOV2017:12:02:00  10NOV2017:18:24:50    6  
1    a1  10NOV2017:12:02:00  10NOV2017:14:02:58    3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   12  
1    a1  10NOV2017:12:02:00  10NOV2017:13:57:12    7  &lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:13:01:42   14  &lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:14:42:25    2 &lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:16:55:32    5 &lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I need to find for every id, opt group (combination) in how many unique hours (column 't_spent') has the&amp;nbsp;record (in past 3 hours). For example, if it's record from 10NOV2017:21:04:33, I need to count a distinct number of hours from&amp;nbsp;&lt;SPAN&gt;10NOV2017:18:04:33 to 10NOV2017:21:04:33&amp;nbsp;if id, opt combination has the&amp;nbsp;record. Min what we can get is 1, max is 3.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I have this code (mostly part is useful and helpful and it's written by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, but the part I've changed is not working very well) where I tried to count distinct of column&amp;nbsp; 'hourIy_time' additionally created&lt;SPAN&gt;&amp;nbsp; hourly_time = t_spent and formatted format hourly_time datetime11. so I could maybe use it to count distinct but this approach is not the best because it can easily catch&amp;nbsp;4 different hours. Also, this way I discovered that data step does not support DISTINCT function.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; _n_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; do&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;rename&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(t_spent&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;_t_spent&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;declare&lt;/SPAN&gt; hash h&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;dataset:&lt;SPAN class="token string"&gt;'have(rename=(t_spent=_t_spent))'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;hashexp:&lt;SPAN class="token number"&gt;20&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definekey&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'id'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'opt'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;'t_spent'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedata&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'_usage'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;definedone&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
 end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token operator"&gt;count_hours=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
do i&lt;SPAN class="token operator"&gt;=t_spent-3*3600&lt;/SPAN&gt; to t_spent &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;find&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key:&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;key:opt&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;key:i&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; count_hours&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;_t_spent&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;drop&lt;/SPAN&gt; _usage i&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Desired output with the desired column:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;id   opt     t_purchase           t_spent        usage  count_hours&lt;BR /&gt;1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00    3         1
1    a1  10NOV2017:12:02:00  10NOV2017:15:20:33    0         2
1    a1  10NOV2017:12:02:00  10NOV2017:18:24:50    6         1
1    a1  10NOV2017:12:02:00  10NOV2017:14:02:58    3         2
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   12         2
1    a1  10NOV2017:12:02:00  10NOV2017:13:57:12    7         1&lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:13:01:42   14         1 &lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:14:42:25    2         2&lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:16:55:32    5         3&lt;BR /&gt;2    b1  10NOV2017:12:55:24  10NOV2017:15:24:07    3         2&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 02 Dec 2017 00:15:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417901#M102650</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-12-02T00:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417929#M102658</link>
      <description>&lt;P&gt;Wouldn' that simply be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select id, opt, count(distinct(hour(t_spent))) as hour_count
from have
group by id, opt;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Sat, 02 Dec 2017 07:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417929#M102658</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-12-02T07:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417930#M102659</link>
      <description>&lt;P&gt;OK. Try this one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  usage ;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00    3  
1    a1  10NOV2017:12:02:00  10NOV2017:15:20:33    0 
1    a1  10NOV2017:12:02:00  10NOV2017:18:24:50    6  
1    a1  10NOV2017:12:02:00  10NOV2017:14:02:58    3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   12  
1    a1  10NOV2017:12:02:00  10NOV2017:13:57:12    7  
2    b1  10NOV2017:12:55:24  10NOV2017:13:01:42   14  
2    b1  10NOV2017:12:55:24  10NOV2017:14:42:25    2 
2    b1  10NOV2017:12:55:24  10NOV2017:16:55:32    5 
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h(dataset:'have',hashexp:20);
  h.definekey('id','opt', 't_spent');
  h.definedone();

  declare hash h_hour();
  h_hour.definekey('hour');
  h_hour.definedone();
 end;
set have;
do i=t_spent-3*3600 to t_spent ;
 if h.check(key:id,key:opt,key:i)=0 then do;hour=hour(i);h_hour.ref();end;
end;
count_hours=h_hour.num_items;
h_hour.clear();
drop hour i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Dec 2017 10:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417930#M102659</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-02T10:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417999#M102680</link>
      <description>&lt;P&gt;Thank you very much. This works just as I needed.&lt;/P&gt;&lt;P&gt;I tasted only on small data, when I tried on 5.5m rows, it's working by now about 6 hours. Is it expected? I thought about splitting into more tables, but that would mean that I need to join them after and that's pretty much the same.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2017 10:02:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/417999#M102680</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-12-03T10:02:19Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418057#M102686</link>
      <description>&lt;P&gt;This runs in 30 seconds for 10 million records.&lt;/P&gt;
&lt;P&gt;&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 ID  OPT  $   T_PURCHASE   : datetime32. T_SPENT :datetime32.  USAGE ;
  format  T_PURCHASE  T_SPENT datetime32. ;
  do ID=1 to 1e6; output; end;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00    3  
1    a1  10NOV2017:12:02:00  10NOV2017:15:20:33    0 
1    a1  10NOV2017:12:02:00  10NOV2017:18:24:50    6  
1    a1  10NOV2017:12:02:00  10NOV2017:14:02:58    3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   12  
1    a1  10NOV2017:12:02:00  10NOV2017:13:57:12    7  
2    b1  10NOV2017:12:55:24  10NOV2017:13:01:42   14  
2    b1  10NOV2017:12:55:24  10NOV2017:14:42:25    2 
2    b1  10NOV2017:12:55:24  10NOV2017:16:55:32    5 
run;

data DIFF;
  do DIFF=0 to 3600*3;
    output; 
  end; 
run;

proc sql;                
  create table WANT as
  select unique det.*, count(sum.T_SPENT) as COUNT
   from HAVE det
       ,DIFF dif
       ,HAVE sum
   where det.ID          =sum.ID 
     and det.OPT         =sum.OPT
     and det.T_SPENT-DIFF=sum.T_SPENT
  group by det.ID , det.OPT, det.T_SPENT  
  order by det.ID , det.OPT, det.T_SPENT ; 
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="327"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;&amp;nbsp;ID&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;OPT&lt;/TD&gt;
&lt;TD width="133"&gt;&amp;nbsp;T_SPENT&lt;/TD&gt;
&lt;TD width="66"&gt;&amp;nbsp;COUNT&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:13:57:12&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:14:02:58&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:14:05:00&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:15:20:33&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:18:24:50&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;a1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:20:22:07&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;b1&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;10NOV2017:13:01:42&lt;/TD&gt;
&lt;TD style="font-weight: 400;"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The key to speed is to use equijoins, hence the intermediate table.&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>Mon, 04 Dec 2017 20:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418057#M102686</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-12-04T20:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418141#M102696</link>
      <description>&lt;P&gt;You could fast it if sorting the table before .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  usage ;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00    3  
1    a1  10NOV2017:12:02:00  10NOV2017:15:20:33    0 
1    a1  10NOV2017:12:02:00  10NOV2017:18:24:50    6  
1    a1  10NOV2017:12:02:00  10NOV2017:14:02:58    3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   12  
1    a1  10NOV2017:12:02:00  10NOV2017:13:57:12    7  
2    b1  10NOV2017:12:55:24  10NOV2017:13:01:42   14  
2    b1  10NOV2017:12:55:24  10NOV2017:14:42:25    2 
2    b1  10NOV2017:12:55:24  10NOV2017:16:55:32    5 
;
run;
proc sort data=have;
 by id  opt;
run;
data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h();
  h.definekey('id','opt', 't_spent');
  h.definedone();

  declare hash h_hour();
  h_hour.definekey('hour');
  h_hour.definedone();
 end;

do until(last.opt);
 set have;
 by id opt;
 h.ref();
end;

do until(last.opt);
 set have;
 by id opt;
 do i=t_spent-3*3600 to t_spent ;
   if h.check(key:id,key:opt,key:i)=0 then do;hour=hour(i);h_hour.ref();end;
 end;
 count_hours=h_hour.num_items;
 output;
end;
h.clear();
h_hour.clear();
drop hour i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Dec 2017 13:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418141#M102696</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-04T13:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418287#M102746</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;This is still several orders of magnitude slower than SQL.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2017 20:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418287#M102746</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-12-04T20:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418458#M102791</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Yeah. You are right . It depends on what OP want.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Dec 2017 12:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/418458#M102791</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-05T12:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420713#M103528</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know this was active days ago, but I just found a little problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both versions give me (mostly) +1 hour that I needed. I know it's a very tricky to solve this because I need to count 3*60 minutes in past from the record and count how many records that combination has in first 60 min, in 120 min, and in 180 min in the past.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, If some combination has the record in 13:40 and 14: 05, for the record from 14:05 count need to be 1, not 2. Record 13:40 was in the past hour (13) but is just 25 minutes before, not the 60+.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you both for your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 09:43:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420713#M103528</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-12-13T09:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420733#M103535</link>
      <description>&lt;P&gt;The count method as given counts 14:05 as the first match and 13:40 as the second match for the record at 14:05, hence you have 2 macthes.&lt;/P&gt;
&lt;P&gt;If you don't want to count 14:05, just subtract 1.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 10:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420733#M103535</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-12-13T10:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420735#M103536</link>
      <description>&lt;P&gt;I'm not sure if you have understood the question, I'm sorry for not being so clear.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In how many hours (in past 3 hours, 180min/3) that combination has the record. Not how many matches(record) has in each hour.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe someone has just the&amp;nbsp;record in 14:03, 14:05, 14:19, 14:55, 15:02, and that need to be count=1. Because the range is &amp;lt;60min and it does not matter how many records have in that range. If the range is between 0min-60min and 60min-120min it is 2 (example 14:01,&amp;nbsp;&lt;SPAN&gt;14:03, 14:05, 14:19, 14:55, 15:02)&amp;nbsp;&lt;/SPAN&gt;, if it's between 0min-60min, 60min-120min and 120min-180min, that's count=3 &lt;SPAN&gt;&amp;nbsp;(example 14:01,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;14:03, 14:05, 14:19, 14:55, 15:02, 16:04)&lt;/SPAN&gt;, because have records in all the 3 past hours.&lt;/P&gt;&lt;P&gt;Range &amp;gt;180min I don't look at all, need to take observation for each record just the past 3 hours.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 11:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420735#M103536</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-12-13T11:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct hours in past 3hr</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420739#M103538</link>
      <description>&lt;P&gt;Something like this (can't test atm) should be close, please run and alter as needed&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;                
  create table WANT as
  select unique det.*
  , sum(sum.T_SPENT*(3600*0&amp;lt;DIFF&amp;lt;=3600*1))&amp;gt;0 as INTERVAL1H
  , sum(sum.T_SPENT*(3600*1&amp;lt;DIFF&amp;lt;=3600*2))&amp;gt;0 as INTERVAL2H
  , sum(sum.T_SPENT*(3600*2&amp;lt;DIFF&amp;lt;=3600*3))&amp;gt;0 as INTERVAL3H
  , sum(calculated INTERVAL1H,calculated INTERVAL2H,calculated INTERVAL3H) as COUNT_THAT_YOU_WANT
   from HAVE det
       ,DIFF dif
       ,HAVE sum
   where det.ID          =sum.ID 
     and det.OPT         =sum.OPT
     and det.T_SPENT-DIFF=sum.T_SPENT
  group by det.ID , det.OPT, det.T_SPENT  
  order by det.ID , det.OPT, det.T_SPENT ; 
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 11:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-hours-in-past-3hr/m-p/420739#M103538</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-12-13T11:10:22Z</dc:date>
    </item>
  </channel>
</rss>

