<?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 time before each record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432005#M280754</link>
    <description>&lt;P&gt;Hi, I know it's been a while, but I must ask you for a favor if you could just write me a short explanation for each line of this hash function. Just u few words. Some of them I really don't get it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assume this is not by rules of community or appropriate, but I have no choice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance and sorry for bothering you.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 Jan 2018 23:04:55 GMT</pubDate>
    <dc:creator>jovic92</dc:creator>
    <dc:date>2018-01-29T23:04:55Z</dc:date>
    <item>
      <title>Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421131#M280751</link>
      <description>&lt;P&gt;I have to distinct count for each record (line) how many datetime (when it's started have) have any combination&amp;nbsp; (grouped by two columns) till that moment.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  bonus  usage ;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3 
1    a1  10NOV2017:15:45:00  10NOV2017:15:07:33   100     0  
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6 
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3 
1    a1  10NOV2017:19:15:00  10NOV2017:20:22:07   100    12    
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, grouped&amp;nbsp;by id and opt, count distinct t_purchase that is not newer than t_purchase of that line.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The desired output would be:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  bonus  usage count_t_purchase;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3    1
1    a1  10NOV2017:15:45:00  10NOV2017:15:07:33   100     0    2
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6    1
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3    1
1    a1  10NOV2017:19:15:00  10NOV2017:20:22:07   100    12    3
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7    1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have tried with the hash table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA
work.WANT;
if _n_=1 then do;
    if 0 then set WORK.HAVE(rename=(t_purchase=_t_purchase));
	declare hash h(dataset: 'WORK.HAVE(rename=(t_purchase=_t_purchase))', hashexp:20);
	h.definekey('id', 'opt', 't_spent');
	h.definedata('t_purchase');
	h.definedone();
	end;
set WORK.HAVE;
COUNT_purchase=0;
do i=MIN(t_purchase) to t_spent;
	if h.find(key:id, key:opt, key:i)=0 then COUNT_purchase+1;
end;
drop _t_purchase i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I got a few error, one of them is that the t_purchase column does not exist in the table HAVE, but it is there.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, the hash table is not the only way. Any kind of solution and help is welcomed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2017 10:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421131#M280751</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-12-14T10:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421133#M280752</link>
      <description>&lt;P&gt;Maybe something like:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  A.*,
          count(distinct cats(B.ID,B.OPT)) as COUNT_T_PURCHASE
  from    HAVE A
  left join HAVE B
  on      A.ID=B.ID
  and     A.OPT=B.OPT
  and     A.T_PURCHASE &amp;gt; B.T_PURCHASE;
quit;&lt;/PRE&gt;
&lt;P&gt;I cant test at the moment, but basically join all data with date less on, then count it.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Dec 2017 10:14:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421133#M280752</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-14T10:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421153#M280753</link>
      <description>&lt;P&gt;OK. How about 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.  bonus  usage ;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3 
1    a1  10NOV2017:15:45:00  10NOV2017:15:07:33   100     0  
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6 
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3 
1    a1  10NOV2017:19:15:00  10NOV2017:20:22:07   100    12    
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7 
;
run;
proc sql;
create table temp as
 select *,min(t_purchase) as min
  from have
   group by id,opt;
quit;
DATA work.WANT;
if _n_=1 then do;
    if 0 then set WORK.temp;
	declare hash h(dataset: 'WORK.temp', hashexp:20);
	h.definekey('id', 'opt', 't_purchase');
	h.definedone();
	end;
set WORK.temp;
COUNT_purchase=0;
do i=MIN to t_purchase;
	if h.check(key:id, key:opt, key:i)=0 then COUNT_purchase+1;
end;
drop  i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Dec 2017 12:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/421153#M280753</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-14T12:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432005#M280754</link>
      <description>&lt;P&gt;Hi, I know it's been a while, but I must ask you for a favor if you could just write me a short explanation for each line of this hash function. Just u few words. Some of them I really don't get it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I assume this is not by rules of community or appropriate, but I have no choice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance and sorry for bothering you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 23:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432005#M280754</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2018-01-29T23:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432113#M280755</link>
      <description>&lt;P&gt;Thats really the function of the documentation:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#n1b4cbtmb049xtn1vh9x4waiioz4.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#n1b4cbtmb049xtn1vh9x4waiioz4.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And there are a lot of papers out there on the subject:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/forum2008/029-2008.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2008/029-2008.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 08:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432113#M280755</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-30T08:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: Count distinct time before each record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432158#M280756</link>
      <description>&lt;P&gt;OK. The code is short .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;/*************&lt;BR /&gt;here pick up the minimal value of t_purchase,&lt;BR /&gt;and data like :&lt;BR /&gt;id ...  min&lt;BR /&gt;1 ....  '01jan2010:10:10:02'dt&lt;BR /&gt;1 ....  '01jan2010:10:10:02'dt&lt;BR /&gt;1 ....  '01jan2010:10:10:02'dt&lt;BR /&gt;&lt;BR /&gt;**************/&lt;BR /&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
create &lt;SPAN class="token statement"&gt;table&lt;/SPAN&gt; temp as
 &lt;SPAN class="token statement"&gt;select&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;t_purchase&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; as &lt;SPAN class="token function"&gt;min&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;from&lt;/SPAN&gt; have
   &lt;SPAN class="token keyword"&gt;group&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;opt&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;quit&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;DATA&lt;/SPAN&gt; work&lt;SPAN class="token punctuation"&gt;.&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; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;temp&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;'WORK.temp'&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;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;/* Here push table temp into Hash Table for querying later &lt;BR /&gt;  key is ID OPT T_PURCHASE */
	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_purchase'&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; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;temp&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
COUNT_purchase&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;  /*for each obs, reset count number to be 0*/&lt;/SPAN&gt;
do i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;MIN&lt;/SPAN&gt; to t_purchase&lt;SPAN class="token punctuation"&gt;; /*for each obs, do loop from min to current t_purchase,step is one second*/&lt;/SPAN&gt;
  /* in Hash Table ,find whether there is an obs or not according to key id,opt,i(i.e. t_purchase)&lt;BR /&gt;     if find it,then count +1 */	&lt;BR /&gt;&lt;SPAN class="token keyword"&gt;  if&lt;/SPAN&gt; h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;check&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_purchase&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&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;drop&lt;/SPAN&gt;  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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 12:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-distinct-time-before-each-record/m-p/432158#M280756</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-30T12:26:26Z</dc:date>
    </item>
  </channel>
</rss>

