<?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: Running count distinct by group through time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438945#M282351</link>
    <description>&lt;P&gt;You can do it with a hash table, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id1 @6 id2 $ @9 time yymmdd8.;
  format time date9.;
cards;
1    A  20131128
1    B  20140214
1    C  20140530
1    C  20140622
1    D  20140831
1    D  20141220
1    A  20150217
1    A  20150302
1    C  20150410
1    D  20150425
1    E  20150609
1    F  20151025
1    C  20160612
1    D  20180101
2    A  19900515
2    B  19900813
2    E  19910522
2    A  19910524
2    F  19910919
2    G  19920101
2    A  19930321
;run;



data want;
  set have;
  by id1;
  if first.id1 then do;
    if _N_=1 then do;
      declare hash h();
      rc=h.definedata('lasttime');
      rc=h.definekey('id2');
      h.definedone();
      declare hiter iter('h');
      end;
    else
      h.clear();
    end;
  rc=h.find();
  lasttime=time;
  if rc then
    h.add();
  else
    h.replace();
  runningcount1=h.num_items;
  rc=iter.first();
  starttime=intnx('year',time,-1,'same');
  runningcount2=0;
  do until(iter.next());
    if lasttime&amp;gt;=starttime then
      runningcount2=runningcount2+1;
    end;
  keep id1 id2 time runningcount1 runningcount2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Feb 2018 15:12:15 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-02-21T15:12:15Z</dc:date>
    <item>
      <title>Running count distinct by group through time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438839#M282349</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, I have&amp;nbsp;&lt;/SPAN&gt;below&lt;SPAN&gt;&amp;nbsp;dataset. I need to count distinct values of id2 by id1 through time. The output I want is the runningcount1 column. Additionally, I need to repeat the&amp;nbsp;same procedure for the last 360 days. Checking last 360 days and counting&amp;nbsp;&lt;/SPAN&gt;distinct&lt;SPAN&gt;&amp;nbsp;number of id2 by id1. If it goes less than 360 days back, then it should count distinct values of id2 by id1 however many days it goes back. The output I want is in&amp;nbsp;&lt;/SPAN&gt;runningcount2&lt;SPAN&gt;&amp;nbsp;column. Data is sorted by id1 and time.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;Thank you very much.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;id1 id2 time&amp;nbsp; &amp;nbsp;runningcount1 runningcount2&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; A&amp;nbsp; 20131128&amp;nbsp; &amp;nbsp;1 1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; B&amp;nbsp; 20140214&amp;nbsp; &amp;nbsp;2 2&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; C&amp;nbsp; 20140530&amp;nbsp; &amp;nbsp;3 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; C&amp;nbsp; 20140622&amp;nbsp; &amp;nbsp;3 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; D&amp;nbsp; 20140831&amp;nbsp; &amp;nbsp;4 4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; D&amp;nbsp; 20141220&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; A&amp;nbsp; 20150217&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; A&amp;nbsp; 20150302&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; C&amp;nbsp; 20150410&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; D&amp;nbsp; 20150425&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; E&amp;nbsp; 20150609&amp;nbsp; &amp;nbsp;5 4&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; F&amp;nbsp; 20151025&amp;nbsp; &amp;nbsp;6 5&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; C&amp;nbsp; 20160612&amp;nbsp; &amp;nbsp;6 2&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; D&amp;nbsp; 20180101&amp;nbsp; &amp;nbsp;6 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; A&amp;nbsp; 19900515&amp;nbsp; &amp;nbsp;1 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; B&amp;nbsp; 19900813&amp;nbsp; &amp;nbsp;2 2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; E&amp;nbsp; 19910522&amp;nbsp; &amp;nbsp;3 2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; A&amp;nbsp; 19910524&amp;nbsp; &amp;nbsp;3 3&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; F&amp;nbsp; 19910919&amp;nbsp; &amp;nbsp;4 3&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; G&amp;nbsp; 19920101&amp;nbsp; &amp;nbsp;5 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; A&amp;nbsp; 19930321&amp;nbsp; &amp;nbsp;5 1&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 02:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438839#M282349</guid>
      <dc:creator>lezgin</dc:creator>
      <dc:date>2018-02-21T02:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: Running count distinct by group through time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438880#M282350</link>
      <description>&lt;P&gt;Please post the contents of the excel-file as data-step using datalines statement, so that we don't have to guess how the data looked like when you imported the file.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 08:04:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438880#M282350</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-02-21T08:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Running count distinct by group through time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438945#M282351</link>
      <description>&lt;P&gt;You can do it with a hash table, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id1 @6 id2 $ @9 time yymmdd8.;
  format time date9.;
cards;
1    A  20131128
1    B  20140214
1    C  20140530
1    C  20140622
1    D  20140831
1    D  20141220
1    A  20150217
1    A  20150302
1    C  20150410
1    D  20150425
1    E  20150609
1    F  20151025
1    C  20160612
1    D  20180101
2    A  19900515
2    B  19900813
2    E  19910522
2    A  19910524
2    F  19910919
2    G  19920101
2    A  19930321
;run;



data want;
  set have;
  by id1;
  if first.id1 then do;
    if _N_=1 then do;
      declare hash h();
      rc=h.definedata('lasttime');
      rc=h.definekey('id2');
      h.definedone();
      declare hiter iter('h');
      end;
    else
      h.clear();
    end;
  rc=h.find();
  lasttime=time;
  if rc then
    h.add();
  else
    h.replace();
  runningcount1=h.num_items;
  rc=iter.first();
  starttime=intnx('year',time,-1,'same');
  runningcount2=0;
  do until(iter.next());
    if lasttime&amp;gt;=starttime then
      runningcount2=runningcount2+1;
    end;
  keep id1 id2 time runningcount1 runningcount2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Feb 2018 15:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/438945#M282351</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-21T15:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Running count distinct by group through time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/439000#M282352</link>
      <description>&lt;P&gt;Thank you very much s_lassen. This was very helpful.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2018 17:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/439000#M282352</guid>
      <dc:creator>lezgin</dc:creator>
      <dc:date>2018-02-21T17:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: Running count distinct by group through time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/439175#M282353</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id1 @6 id2 $ @9 time yymmdd8.;
  format time date9.;
cards;
1    A  20131128
1    B  20140214
1    C  20140530
1    C  20140622
1    D  20140831
1    D  20141220
1    A  20150217
1    A  20150302
1    C  20150410
1    D  20150425
1    E  20150609
1    F  20151025
1    C  20160612
1    D  20180101
2    A  19900515
2    B  19900813
2    E  19910522
2    A  19910524
2    F  19910919
2    G  19920101
2    A  19930321
;run;
proc sql;
select *,(select count(distinct id2) from have where id1=a.id1 and time le a.time) as count1,
(select count(distinct id2) from have where id1=a.id1 and time between a.time-360 and a.time) as count2
 from have as a;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Feb 2018 06:05:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-count-distinct-by-group-through-time/m-p/439175#M282353</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-02-22T06:05:12Z</dc:date>
    </item>
  </channel>
</rss>

