<?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 of observations by id and date if there are 4 or more transactions in a 2 day window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316685#M69251</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id transactiondate :ddmmyy10.;
format transactiondate ddmmyy10.;
cards;
1          01/11/2016
1          01/11/2016
1          02/11/2016
1          02/11/2016
2          01/11/2016
2          07/11/2016
3          03/11/2016
3          04/11/2016
3          04/11/2016
3          04/11/2016
;
run;

/* First accumulate per day */
proc sql;
create table int as
select id, transactiondate, count(*) as count
from have
group by id,transactiondate
;
quit;

/* Now compare with previous observation */
data want;
set int;
by id;
lastdate = lag(transactiondate);
format lastdate ddmmyy10.;
lastcount = lag(count);
if lastdate = transactiondate - 1
then do;
  count = count + lastcount;
  output;
end;
keep id transactiondate count lastdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 05 Dec 2016 13:15:55 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-12-05T13:15:55Z</dc:date>
    <item>
      <title>Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316681#M69249</link>
      <description>&lt;P&gt;Hi there, sorry second question of the day today.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a large data set with numerous variables two of which are "ID" and "TransactionDate"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what I want to do is count the number of transactions in two day windows per id&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TransactionDate &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/11/2016 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/11/2016&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/11/2016&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/11/2016&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01/11/2016&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;07/11/2016&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/11/2016&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/11/2016&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/11/2016&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;04/11/2016&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I only want to see ID 1 and 3 as they have 4 records in a two day window.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've had a play around with proq freq and the dif and Lag functions but I just cant seem to work it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Marc&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, 05 Dec 2016 13:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316681#M69249</guid>
      <dc:creator>Stretlow</dc:creator>
      <dc:date>2016-12-05T13:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316684#M69250</link>
      <description>&lt;P&gt;Well, step 1 is to identify records which conform to your "2 day window" criteria. &amp;nbsp;You would need to flesh this out for me a bit, is it 2 days from earliest date, 2 days from previous etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Wonce you have that then a simple count() group by id &amp;lt;var&amp;gt;; would work.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2016 13:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316684#M69250</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-12-05T13:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316685#M69251</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id transactiondate :ddmmyy10.;
format transactiondate ddmmyy10.;
cards;
1          01/11/2016
1          01/11/2016
1          02/11/2016
1          02/11/2016
2          01/11/2016
2          07/11/2016
3          03/11/2016
3          04/11/2016
3          04/11/2016
3          04/11/2016
;
run;

/* First accumulate per day */
proc sql;
create table int as
select id, transactiondate, count(*) as count
from have
group by id,transactiondate
;
quit;

/* Now compare with previous observation */
data want;
set int;
by id;
lastdate = lag(transactiondate);
format lastdate ddmmyy10.;
lastcount = lag(count);
if lastdate = transactiondate - 1
then do;
  count = count + lastcount;
  output;
end;
keep id transactiondate count lastdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Dec 2016 13:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316685#M69251</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-05T13:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316720#M69256</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id transactiondate :ddmmyy10.;
format transactiondate ddmmyy10.;
cards;
1          01/11/2016
1          01/11/2016
1          02/11/2016
1          02/11/2016
2          01/11/2016
2          07/11/2016
3          03/11/2016
3          04/11/2016
3          04/11/2016
3          04/11/2016
;
run;

/* First accumulate per day */
proc sql;
create table int as
select id, transactiondate, count(*) as count
from have
group by id,transactiondate
;
quit;

/* Now compare with previous observation */
data want;
set int;
by id;
lastdate = lag(transactiondate);
format lastdate ddmmyy10.;
lastcount = lag(count);
if lastdate = transactiondate - 1
then do;
  count = count + lastcount;
  output;
end;
keep id transactiondate count lastdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for this. the second part of the code there is what i was trying to do but i was a little out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks to be working in the main however I have found an example where there are 4 observations on one day that are not in the last output dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you know why this would be?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stret&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2016 15:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316720#M69256</guid>
      <dc:creator>Stretlow</dc:creator>
      <dc:date>2016-12-05T15:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316725#M69258</link>
      <description>&lt;P&gt;I see. I am specifically checking for two days with data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set int;
by id;
lastdate = lag(transactiondate);
format lastdate ddmmyy10.;
lastcount = lag(count);
if lastdate = transactiondate - 1
then do;
  count = count + lastcount;
  if count &amp;gt;= 4 then output;
end;
else do;
  if count &amp;gt;= 4 then output;
end;
keep id transactiondate count lastdate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might get redundant entries if you have a sequence like 3-3-3 on three successive days.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2016 15:18:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316725#M69258</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-05T15:18:53Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316726#M69259</link>
      <description>&lt;P&gt;If your data are sorted by id/transaction date, then this will be efficient and simple:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The do loop contructs a count of transactions (_NTRANS) for a single day&lt;/LI&gt;
&lt;LI&gt;Then a two-record&amp;nbsp;count is calculated&lt;/LI&gt;
&lt;LI&gt;Only if the prior record was one day preceding the current record, keep it&lt;/LI&gt;
&lt;LI&gt;The first program, however, will output overlapping two-day windows (i.e. 3 consecutive dates).&amp;nbsp; If you don't&amp;nbsp; want that, use the second program&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do _ntrans=1 by 1 until (last.transactiondate);
     set have;
     by id transactiondate ;
  end;

  twoday_count=sum(_ntrans,lag(_ntrans));
  if dif(transactiondate)=1;
run;
&lt;/CODE&gt;&lt;/PRE&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=_:);
  retain _last_windowdate;  format _last_windowdate yymmddn8.;
  do _ntrans=1 by 1 until (last.transactiondate);
     set have;
     by id transactiondate ;
      if first.id then _last_windowdate=.;
  end;
  twoday_count=sum(_ntrans,lag(_ntrans));
  if dif(transactiondate)=1  and (_last_windowdate^=transactiondate-2);
  _last_windowdate=transactiondate;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2016 15:19:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316726#M69259</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-05T15:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316748#M69266</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;If your data are sorted by id/transaction date, then this will be efficient and simple:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The do loop contructs a count of transactions (_NTRANS) for a single day&lt;/LI&gt;
&lt;LI&gt;Then a two-record&amp;nbsp;count is calculated&lt;/LI&gt;
&lt;LI&gt;Only if the prior record was one day preceding the current record, keep it&lt;/LI&gt;
&lt;LI&gt;The first program, however, will output overlapping two-day windows (i.e. 3 consecutive dates).&amp;nbsp; If you don't&amp;nbsp; want that, use the second program&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  do _ntrans=1 by 1 until (last.transactiondate);
     set have;
     by id transactiondate ;
  end;

  twoday_count=sum(_ntrans,lag(_ntrans));
  if dif(transactiondate)=1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This again seems to be working fine other than the anomoly i posted above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have one particular ID that has 8 Observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4 on the 18/11/2016&lt;/P&gt;
&lt;P&gt;2 on the 29/11/2016&lt;/P&gt;
&lt;P&gt;2 on the 30/11/2016&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Its output the 29th and 30th however not the 4 on the 18th.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the help&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 05 Dec 2016 15:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316748#M69266</guid>
      <dc:creator>Stretlow</dc:creator>
      <dc:date>2016-12-05T15:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316799#M69282</link>
      <description>&lt;P&gt;Oops sorry.&amp;nbsp; I see you mean 4 trans&amp;nbsp; WITHIN two days, even if only one of those days is present:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, notice the "trick" here is to update the LAG/DIF queues only at the end of each day's set of transactions.&amp;nbsp; Also, even though the "lag(_ntrans)" is inside an IFN function, the lag queue is updated regardless of whether the ifn test is true.&amp;nbsp; Which is what you&amp;nbsp;want - the lag queue is always updated even if its results are not returned by the IFN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;do&amp;nbsp;_ntrans=1 by 1 until(last.transactiondate);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id transactiondate;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;total_count = sum(_ntrans,ifn(dif(transactiondate)=1 and dif(id)=0,lag(_ntrans),0));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if total_count&amp;gt;=4;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Dec 2016 18:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316799#M69282</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-05T18:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316962#M69339</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Oops sorry.&amp;nbsp; I see you mean 4 trans&amp;nbsp; WITHIN two days, even if only one of those days is present:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, notice the "trick" here is to update the LAG/DIF queues only at the end of each day's set of transactions.&amp;nbsp; Also, even though the "lag(_ntrans)" is inside an IFN function, the lag queue is updated regardless of whether the ifn test is true.&amp;nbsp; Which is what you&amp;nbsp;want - the lag queue is always updated even if its results are not returned by the IFN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;do&amp;nbsp;_ntrans=1 by 1 until(last.transactiondate);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by id transactiondate;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;total_count = sum(_ntrans,ifn(dif(transactiondate)=1 and dif(id)=0,lag(_ntrans),0));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if total_count&amp;gt;=4;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is making a bit more sense now and I think we're almost there with it.&lt;/P&gt;
&lt;P&gt;heres a sample of the output I have:-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="295"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;ID&lt;/TD&gt;
&lt;TD width="108"&gt;TransactionDate&lt;/TD&gt;
&lt;TD width="78"&gt;total_count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510110787&lt;/TD&gt;
&lt;TD width="108"&gt;18/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510110787&lt;/TD&gt;
&lt;TD width="108"&gt;30/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510141720&lt;/TD&gt;
&lt;TD width="108"&gt;26/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510390852&lt;/TD&gt;
&lt;TD width="108"&gt;21/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510390852&lt;/TD&gt;
&lt;TD width="108"&gt;22/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510390852&lt;/TD&gt;
&lt;TD width="108"&gt;28/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="109"&gt;1510472816&lt;/TD&gt;
&lt;TD width="108"&gt;20/11/2016&lt;/TD&gt;
&lt;TD width="78"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you touched on this earler in the thread about overlapping days.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For ID&amp;nbsp;&lt;SPAN&gt;1510390852 there are 5&amp;nbsp;observations&amp;nbsp;on the 21/11/2016 and 2 on the 22/11/2016.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Instead of the output being 5 and 7 for the 21st and 22nd, as these are within a two day window would just like to see 1&amp;nbsp;observation with a value of 7.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Is this possible?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Obviously for the first Id in that table as the 18th and the 30th arent within a two day window the 2 obervations with a value of 4 are correct.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Your continued help is appreciated&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Stret&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2016 09:23:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/316962#M69339</guid>
      <dc:creator>Stretlow</dc:creator>
      <dc:date>2016-12-06T09:23:59Z</dc:date>
    </item>
    <item>
      <title>Re: Count of observations by id and date if there are 4 or more transactions in a 2 day window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/317121#M69388</link>
      <description>&lt;P&gt;You want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For any pair of consecutive days output total transactions if the total&amp;gt;=4.&amp;nbsp; This just requires looking back one record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For any single day not part of such a pair output total transactions if the total&amp;gt;=4.&amp;nbsp; this will require looking ahead one record to see if the next record is a consecutive date.&amp;nbsp; If not it's a singel day window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now what if you have 3 consecutive&amp;nbsp; days each with 3 transactions?&amp;nbsp;&amp;nbsp; output the first pair and ignore the 3rd?&amp;nbsp; If you have 4 consecutive days with&amp;nbsp; 3 transactions each, presumably you output two non-overlapping windows each with 6 transactions. Is that correct?&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_: nxt_:);

  do _ntrans=1 by 1 until(last.transactiondate);
    set have;
    by id transactiondate;
    if eoh=0 then set have 
      (firstobs=2 keep=id transactiondate rename=(id=nxt_id  transactiondate=nxt_td))
      end=eoh;
  end;

  _records_since_output+1;

  /* Only add the lagged _ntrans if previous rec is one day prior */
  total_count = sum(_ntrans,ifn(dif(id)=0 and dif(transactiondate)=1,lag(_ntrans),0));

  nxt_day_present=(id=nxt_id and transactiondate+1=nxt_td);

  if total_count&amp;gt;=4 and (nxt_day_present=0 or _records_since_output&amp;gt;=2);
  _records_since_output=0;
run;
&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, 07 Dec 2016 02:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-of-observations-by-id-and-date-if-there-are-4-or-more/m-p/317121#M69388</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-07T02:39:03Z</dc:date>
    </item>
  </channel>
</rss>

