<?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: Counting the number of event trigger over a pre-defined period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951800#M372042</link>
    <description>&lt;P&gt;So let's look at the first 7 observations for ID=1.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1732541639977.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102488i2F5389978BE43D1F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1732541639977.png" alt="Tom_0-1732541639977.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You can see there are two events where the HAVE flag transitioned from 0 to 1.&lt;/P&gt;
&lt;P&gt;The first one on 01FEB2012 and the second on 01JUN2012.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both are within the 6 month window for the 7th observation on 01JUL2012.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I repeat my question from before:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Your results seem to be for a 5 month window, not 6.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it on purpose?&amp;nbsp; &amp;nbsp;Why do you want to exclude events that occur in the first month of the window?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
    <pubDate>Mon, 25 Nov 2024 13:40:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-11-25T13:40:41Z</dc:date>
    <item>
      <title>Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951341#M371902</link>
      <description>&lt;P&gt;Hi sas experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a solution that starts from a dummy variable and counts the number of changes from value 0 to 1 during a pre-defined time window (in my case: 6 months) for each subject (id column).&amp;nbsp; The below example shows how the data that I have looks like, (including the col_have) and the outcome column that I want (col_want):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data data;&lt;BR /&gt;input id time date9. col_have col_want;&lt;BR /&gt;format time date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 01DEC2012 1 2&lt;BR /&gt;1 01NOV2012 0 1&lt;BR /&gt;1 01OCT2012 1 2&lt;BR /&gt;1 01SEP2012 0 1&lt;BR /&gt;1 01AUG2012 1 1&lt;BR /&gt;1 01JUL2012 1 1&lt;BR /&gt;1 01JUN2012 1 2&lt;BR /&gt;1 01MAY2012 0 1&lt;BR /&gt;1 01APR2012 0 1&lt;BR /&gt;1 01MAR2012 1 1&lt;BR /&gt;1 01FEB2012 1 1&lt;BR /&gt;1 01JAN2012 0 0&lt;BR /&gt;2 01DEC2013 0 0&lt;BR /&gt;2 01NOV2013 0 0&lt;BR /&gt;2 01OCT2013 0 1&lt;BR /&gt;2 01SEP2013 0 1&lt;BR /&gt;2 01AUG2013 0 1&lt;BR /&gt;2 01JUL2013 0 2&lt;BR /&gt;2 01JUN2013 1 2&lt;BR /&gt;2 01MAY2013 0 1&lt;BR /&gt;2 01APR2013 1 1&lt;BR /&gt;2 01MAR2013 1 1&lt;BR /&gt;2 01FEB2013 0 0&lt;BR /&gt;2 01JAN2013 1 0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To give a bit of details, the algo should take each date and count the number of times for which the col_have changes values from 0 to 1 during the last 6 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much in advance for effort and support!&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 14:28:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951341#M371902</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-20T14:28:43Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951347#M371903</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=data;
    by id time;
run;
data want;
    set data(drop=col_want);
    by id;
    if first.id then seq=0;
    seq+1;
    prev=ifn(seq&amp;gt;1,lag(col_have),.); 
    prev2=ifn(seq&amp;gt;2,lag2(col_have),.); 
    prev3=ifn(seq&amp;gt;3,lag3(col_have),.); 
    prev4=ifn(seq&amp;gt;4,lag4(col_have),.); 
    prev5=ifn(seq&amp;gt;5,lag5(col_have),.); 
    col_want=(prev=0 and col_have=1) + (prev2=0 and prev=1) + (prev3=0 and prev2=1) + 
        (prev4=0 and prev3=1) + (prev5=0 and prev4=1); 
    drop seq prev:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Nov 2024 15:04:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951347#M371903</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-20T15:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951353#M371905</link>
      <description>&lt;P&gt;Thanks for the solution. I am looking for something more automatic that can be adjusted to different time windows (I.e. 1 year, 2 year). I am confident that sas allows for a more efficient approach. Problem is still open for suggestions.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 15:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951353#M371905</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-20T15:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951357#M371906</link>
      <description>&lt;P&gt;You can turn this into a macro where you specify the number of months.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 16:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951357#M371906</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-11-20T16:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951364#M371907</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465691"&gt;@victor1893&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an algorithm using an arbitrary number of months (macro variable &lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt;), &lt;FONT face="courier new,courier"&gt;&amp;amp;n&amp;gt;1&lt;/FONT&gt;, as the length of the time window:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=data(drop=col_want) out=have;
by id time;
run;

%let n=6; /* length of time window in months */

data want(drop=_:);
array _c[0:%eval(&amp;amp;n-2)] _temporary_;
set have;
by id;
_prev=lag(col_have);
if first.id then call missing(of _c[*]);
else do;
  _m+1;
  _c[mod(_m-1,%eval(&amp;amp;n-1))]=ifn(_prev=0 &amp;amp; col_have=1,1,.);
end;
col_want=n(of _c[*]);
run;

/* Optional: */

proc sort data=want;
by id descending time;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It still assumes that the values of variable TIME are a contiguous series of months for each ID, so that counting observations is equivalent to counting months.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Nov 2024 16:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951364#M371907</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-11-20T16:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951426#M371930</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data;
input id time date9. col_have ;
format time date9.;
datalines;
1 01DEC2012 1 2
1 01NOV2012 0 1
1 01OCT2012 1 2
1 01SEP2012 0 1
1 01AUG2012 1 1
1 01JUL2012 1 1
1 01JUN2012 1 2
1 01MAY2012 0 1
1 01APR2012 0 1
1 01MAR2012 1 1
1 01FEB2012 1 1
1 01JAN2012 0 0
2 01DEC2013 0 0
2 01NOV2013 0 0
2 01OCT2013 0 1
2 01SEP2013 0 1
2 01AUG2013 0 1
2 01JUL2013 0 2
2 01JUN2013 1 2
2 01MAY2013 0 1
2 01APR2013 1 1
2 01MAR2013 1 1
2 01FEB2013 0 0
2 01JAN2013 1 0
;
run;
proc sort data=data out=temp;
by id time;
run;
data temp2;
 set temp;
 flag=( id=lag(id) and lag(col_have)=0 and col_have=1 );
run;
proc sql;
create table want as
select id,time,col_have,(select sum(flag) from temp2 where id=a.id and time between intnx('month',a.time,-5,'s') and a.time) as want
 from temp2 as a
  order by id,time desc;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2024 02:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951426#M371930</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-21T02:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951442#M371937</link>
      <description>&lt;P&gt;Thanks for the solution. Works as expected.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 08:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951442#M371937</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-21T08:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951443#M371938</link>
      <description>&lt;P&gt;As much as I wished that this simpler approach would work as good as the accepted solution, the result is not correct. I appreciate your effort! Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 08:30:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951443#M371938</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-21T08:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951449#M371939</link>
      <description>&lt;P&gt;You're welcome. I've just noticed that, for a very minor performance improvement and code simplification, you can replace the &lt;FONT face="courier new,courier"&gt;_m-1&lt;/FONT&gt; in the first argument of the MOD function by simply &lt;FONT face="courier new,courier"&gt;_m&lt;/FONT&gt;&amp;nbsp;without changing the results:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT color="#999999"&gt;_c[mod(&lt;STRONG&gt;&lt;FONT color="#000000"&gt;_m&lt;/FONT&gt;&lt;/STRONG&gt;,%eval(&amp;amp;n-1))]=ifn(_prev=0 &amp;amp; col_have=1,1,.);&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2024 08:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951449#M371939</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-11-21T08:53:10Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951698#M371997</link>
      <description>&lt;P&gt;I misunderstood what you mean. Here is another Hash Table way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id time date9. col_have col_want;
format time date9.;
datalines;
1 01DEC2012 1 2
1 01NOV2012 0 1
1 01OCT2012 1 2
1 01SEP2012 0 1
1 01AUG2012 1 1
1 01JUL2012 1 1
1 01JUN2012 1 2
1 01MAY2012 0 1
1 01APR2012 0 1
1 01MAR2012 1 1
1 01FEB2012 1 1
1 01JAN2012 0 0
2 01DEC2013 0 0
2 01NOV2013 0 0
2 01OCT2013 0 1
2 01SEP2013 0 1
2 01AUG2013 0 1
2 01JUL2013 0 2
2 01JUN2013 1 2
2 01MAY2013 0 1
2 01APR2013 1 1
2 01MAR2013 1 1
2 01FEB2013 0 0
2 01JAN2013 1 0
;
run;
%let n=6;
data want;
if _n_=1 then do;
 if 0 then set have(rename=(col_have=_col_have));
 declare hash h(dataset:'data(rename=(col_have=_col_have))',hashexp:20);
 h.definekey('id','time');
 h.definedata('_col_have');
 h.definedone();
end;
set have;
want=0;
do i=-%eval(&amp;amp;n.-2) to 0;
 call missing(lag,current);
 if h.find(key:id,key:intnx('month',time,i-1))=0 then lag=_col_have;
 if h.find(key:id,key:intnx('month',time,i))=0 then current=_col_have;
 if lag=0 and current=1 then want+1;
end;
drop i lag current _col_have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Nov 2024 06:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951698#M371997</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-11-23T06:53:57Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951712#M372003</link>
      <description>&lt;P&gt;Your results seem to be for a 5 month window, not 6.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it on purpose?&amp;nbsp; &amp;nbsp;Why do you want to exclude events that occur in the first month of the window?&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>Sun, 24 Nov 2024 04:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951712#M372003</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-24T04:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951740#M372014</link>
      <description>&lt;P&gt;I realize you already have a designated solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But because it utilizes two proc sorts plus a data step, here is a single data step that avoids the sorts.&amp;nbsp; Instead, it reads each ID group twice, first to generate the count of events over 6 months windows, and the second time to assign those counts to the appropriate observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data data;
input id time date9. col_have col_want;
format time date9.;
datalines;
1 01DEC2012 1 2
1 01NOV2012 0 1
1 01OCT2012 1 2
1 01SEP2012 0 1
1 01AUG2012 1 1
1 01JUL2012 1 1
1 01JUN2012 1 2
1 01MAY2012 0 1
1 01APR2012 0 1
1 01MAR2012 1 1
1 01FEB2012 1 1
1 01JAN2012 0 0
2 01DEC2013 0 0
2 01NOV2013 0 0
2 01OCT2013 0 1
2 01SEP2013 0 1
2 01AUG2013 0 1
2 01JUL2013 0 2
2 01JUN2013 1 2
2 01MAY2013 0 1
2 01APR2013 1 1
2 01MAR2013 1 1
2 01FEB2013 0 0
2 01JAN2013 1 0
run;


data want (drop=_: i);
  set data (in=firstpass)  data (in=secondpass);
  by id;

  array ev_count{24} _temporary_;  /*Let array dimension accommodate longest ID sequence*/

  if first.id then call missing (_n1,_n2,of ev_count{*});

  if firstpass then do;
    _n1+1;
    if first.id=0 and col_have=0 and lag(col_have)=1 then do i=max(1,_n1-5) to _n1-1;
      ev_count{i}=sum(ev_count{i},1);
    end;
  end;

  if secondpass;
  _n2+1;
  n_event=sum(0,ev_count{_n2});
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2024 00:47:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951740#M372014</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-11-25T00:47:27Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951753#M372018</link>
      <description>&lt;P&gt;The time windows starts with the current month + 5 additional months from the past which results in 6.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 08:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951753#M372018</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-25T08:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951754#M372019</link>
      <description>&lt;P&gt;I confirm that this solution is correct. I still prefer the initial chosen solution as it is more close to the traditional sas language that I (at least) am more familiar with. Nonetheless, I want to thank you for the effort which is much appreciated. I will try to read more about these hash approaches. Cheers!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 08:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951754#M372019</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-25T08:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951756#M372020</link>
      <description>&lt;P&gt;Hi, thanks for the solution. Result is correct. Even thought the chosen solution uses 2 additional proc sorts, i find it more straightforward and easy to understand. Of course this is a subjective judgement based on my individual sas experience, but in such cases I would prefer a solution that Is more easily understandable and debug-able if of course the difference in performance is not significant. Nonetheless, I very much appreciate your effort for the solution. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 08:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951756#M372020</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-25T08:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951795#M372039</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/465691"&gt;@victor1893&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The time windows starts with the current month + 5 additional months from the past which results in 6.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So your posted expected values are wrong???&lt;/P&gt;
&lt;P&gt;Let's simplify your variable names, so the date value is in a variable named DATE and expected result is in a variable named EXPECT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :date. have expect;
  format date date9.;
datalines;
1 01DEC2012 1 2
1 01NOV2012 0 1
1 01OCT2012 1 2
1 01SEP2012 0 1
1 01AUG2012 1 1
1 01JUL2012 1 1
1 01JUN2012 1 2
1 01MAY2012 0 1
1 01APR2012 0 1
1 01MAR2012 1 1
1 01FEB2012 1 1
1 01JAN2012 0 0
2 01DEC2013 0 0
2 01NOV2013 0 0
2 01OCT2013 0 1
2 01SEP2013 0 1
2 01AUG2013 0 1
2 01JUL2013 0 2
2 01JUN2013 1 2
2 01MAY2013 0 1
2 01APR2013 1 1
2 01MAR2013 1 1
2 01FEB2013 0 0
2 01JAN2013 1 0
;

proc sort;
  by id date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is a method to finds the events and uses a simple series of assignments to create the events from this month EVENT1 to 5 mounts ago EVENT6 on every observation.&amp;nbsp; Then let's use the SUM() function to find the count over 6 months and 5 months and compare that to the expected count.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do month=1 by 1 until (last.id);
    set have;
    by id;
    length sum5 sum6 event1-event6 8;
    event1=have and not lag(have) and not first.id;
    sum5=sum(of event1-event5);
    sum6=sum(of event1-event6);
    output;
    event6=event5;
    event5=event4;
    event4=event3;
    event3=event2;
    event2=event1;
  end;
run;

proc print;
 where sum6 ne expect;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results show that the expected count is one less than the number of events when there is an event in that 6th month.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1732539443813.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102486i2261A014D575DED8/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1732539443813.png" alt="Tom_0-1732539443813.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;full results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1732539527488.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102487i8F09A3279C1EEB1C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_1-1732539527488.png" alt="Tom_1-1732539527488.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 12:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951795#M372039</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-25T12:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951797#M372040</link>
      <description>&lt;P&gt;Hi Tom, apologies for my limited sas knowledge as I find it difficult to understand the logic of the comparison table you produced. But I had a look at the observations highlighted the result not being correct, I can confirm that this is not the case. For example in case of id 1 in 01JUL2012, we look from 01JUL2012 to 01FEB2012 including. The have column changed from 0 to 1 only once in 01JUN2012.&lt;/P&gt;&lt;P&gt;In the second case for id 1 in 01NOV2012 we look from&amp;nbsp;01NOV2012 to 01JUN2012 including. The have column change from 0 to 1 once in 01OCT2012. I see no issue with the provided expected column.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 13:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951797#M372040</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-25T13:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951800#M372042</link>
      <description>&lt;P&gt;So let's look at the first 7 observations for ID=1.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1732541639977.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/102488i2F5389978BE43D1F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1732541639977.png" alt="Tom_0-1732541639977.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You can see there are two events where the HAVE flag transitioned from 0 to 1.&lt;/P&gt;
&lt;P&gt;The first one on 01FEB2012 and the second on 01JUN2012.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both are within the 6 month window for the 7th observation on 01JUL2012.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I repeat my question from before:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Your results seem to be for a 5 month window, not 6.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it on purpose?&amp;nbsp; &amp;nbsp;Why do you want to exclude events that occur in the first month of the window?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Mon, 25 Nov 2024 13:40:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951800#M372042</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-11-25T13:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of event trigger over a pre-defined period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951805#M372045</link>
      <description>&lt;P&gt;I think I understand your judgement, and if not, then please correct me.&lt;/P&gt;&lt;P&gt;You are saying that for 01JUL2012 you should have 2 change events but you only see 1 because the observation in 01JAN2012 is not included in the 6 months window of&amp;nbsp;01JUL2012. The change from 0 to 1 must capture both 0 and 1 in the same window to be accounted for, whereas for 01JUL2012 the id is seen as "starting" with 1 in 01FEB2012. I hope this makes it clear and apologies for the confusion.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 14:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-event-trigger-over-a-pre-defined-period/m-p/951805#M372045</guid>
      <dc:creator>victor1893</dc:creator>
      <dc:date>2024-11-25T14:06:00Z</dc:date>
    </item>
  </channel>
</rss>

