<?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: Cumulative Amount of consecutive days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885483#M349899</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11100"&gt;@Zatere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have done a few testing and I think that it works! Thanks a lot.&lt;/P&gt;
&lt;P&gt;It is also quite efficient in large volume of data.&lt;/P&gt;
&lt;P&gt;I will leave the post open for a day or so, because if you need to do the same piece of analysis for say 15 consecutive days then you will have to add the statements and calculations accordingly which is not very flexible.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you want maybe some other fixed limit than 3.&amp;nbsp; I already sent code to do accumulations over any number of consecutive days.&amp;nbsp; But if you still need a &lt;EM&gt;&lt;STRONG&gt;fixed&lt;/STRONG&gt;&lt;/EM&gt; upper limit on size-of-consecutive-sequence, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ DT:date9. Amount Consecutive_Amount;
format DT date9.;
datalines;
A 09JUL2021 3600 3600
A 03AUG2021 456 489
A 04AUG2021 33 33
A 06AUG2021 235 335
A 07AUG2021 100 100
A 09AUG2021 86 86
A 12AUG2021 456 456
A 24AUG2021 22 1925
A 25AUG2021 987 1984
A 26AUG2021 916 997
A 27AUG2021 81 81
B 07AUG2021 554 992
B 08AUG2021 193 438
B 09AUG2021 245 245
run;
data want (drop=group_size nxt_:);

  /* Read and increment TOTAL until a date gap or next id is upcoming or group_size limit */
  merge have   have (firstobs=2 keep=id dt rename=(id=nxt_id dt=nxt_dt));
  total+amount;
  group_size+1;
  /*Then reread the same observations, output, and decrement TOTAL */
  if id^=nxt_id or dt+1 ^= nxt_dt or group_size=3 then do until (group_size=0);
    set have;
    output;
    total=total-amount;
    group_size=group_size-1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All you have to do is change the number 3 in the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if id^=nxt_id or dt+1 ^= nxt_dt or group_size=3 then do until (group_size=0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement to whatever fixed upper size limit you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this program passes through each group twice (where group "size" can be from 1 to 3 observations in your original specification).&amp;nbsp; The first pass builds the group total and looks for gaps or fixed group size limits.&amp;nbsp; The second re-reads the data, outputs it, and decrements the total.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2023 16:52:05 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-07-19T16:52:05Z</dc:date>
    <item>
      <title>Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885356#M349837</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I would ask for your help on the below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to find the sum of a values for X consecutive days by ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consecutive days means days that happen one after the other without breaks. The program should be able to do so for different X consecutive days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, see the data below:&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt;input ID $ DT:date9. Amount Consecutive_Amount;&lt;BR /&gt;format DT date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;A 09JUL2021 3600 3600&lt;BR /&gt;A 03AUG2021 456 489&lt;BR /&gt;A 04AUG2021 33 33&lt;BR /&gt;A 06AUG2021 235 335&lt;BR /&gt;A 07AUG2021 100 100&lt;BR /&gt;A 09AUG2021 86 86&lt;BR /&gt;A 12AUG2021 456 456&lt;BR /&gt;A 24AUG2021 22 1925&lt;BR /&gt;A 25AUG2021 987 1984&lt;BR /&gt;A 26AUG2021 916 997&lt;BR /&gt;A 27AUG2021 81 81&lt;BR /&gt;B 07AUG2021 554 992&lt;BR /&gt;B 08AUG2021 193 438&lt;BR /&gt;B 09AUG2021 245 245&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Zatere_1-1689710845913.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85902i9F361A76377FDA5C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Zatere_1-1689710845913.png" alt="Zatere_1-1689710845913.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assume that we want to sum the amount for next 3 consecutive days.&lt;/P&gt;
&lt;P&gt;On 24/08/2021 the amount for the next 3 consecutive days is 1925.&lt;BR /&gt;On 07/08/2021, we cannot calculate the amount for the next 3 consecutive days because there is a gap between&amp;nbsp;07/08/2021 and&amp;nbsp;09/08/2021.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any assistance would be much appreciated.&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>Tue, 18 Jul 2023 20:10:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885356#M349837</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2023-07-18T20:10:14Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885366#M349842</link>
      <description>&lt;P&gt;Is it always 3 consecutive that is of interest? If so, the solution is shown below. If it could be 3 when you run the program today, and 12 when you run the program tomorrow, that's much more difficult.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    merge have have(firstobs=2 rename=(id=id1 dt=dt1 amount=amount1)) 
    	have(firstobs=3 rename=(id=id2 dt=dt2 amount=amount2));
	if id^=id1 or dt1-dt^=1 then consecutive_amount=amount;
	if id=id1 and dt1-dt=1 then do;
		consecutive_amount=sum(amount,amount1);
		if id1=id2 and dt2-dt1=1 then consecutive_amount=sum(consecutive_amount,amount2);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jul 2023 21:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885366#M349842</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-18T21:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885374#M349847</link>
      <description>&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input ID $ DT:date9. Amount Consecutive_Amount;&lt;/P&gt;
&lt;P&gt;format DT date9.;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;A 09JUL2021 3600 3600&lt;/P&gt;
&lt;P&gt;A 03AUG2021 456 489&lt;/P&gt;
&lt;P&gt;A 04AUG2021 33 33&lt;/P&gt;
&lt;P&gt;A 06AUG2021 235 335&lt;/P&gt;
&lt;P&gt;A 07AUG2021 100 100&lt;/P&gt;
&lt;P&gt;A 09AUG2021 86 86&lt;/P&gt;
&lt;P&gt;A 12AUG2021 456 456&lt;/P&gt;
&lt;P&gt;A 24AUG2021 22 1925&lt;/P&gt;
&lt;P&gt;A 25AUG2021 987 1984&lt;/P&gt;
&lt;P&gt;A 26AUG2021 916 997&lt;/P&gt;
&lt;P&gt;A 27AUG2021 81 81&lt;/P&gt;
&lt;P&gt;B 07AUG2021 554 992&lt;/P&gt;
&lt;P&gt;B 08AUG2021 193 438&lt;/P&gt;
&lt;P&gt;B 09AUG2021 245 245&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*using lag function to check consecutive days*/&lt;/P&gt;
&lt;P&gt;data have1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; format lagdt1 lagdt2 date9.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; array x(*) lagdt1-lagdt2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; lagdt1=lag1(dt);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; lagdt2=lag2(dt);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.id then count=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; do i=count to dim(x);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x(i)=.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; count+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if n(dt, lagdt1)=2 then diff1=dt-lagdt1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if n(lagdt1, lagdt2)=2 then diff2=lagdt1-lagdt2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data consec1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if diff1=1 and diff2=1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;proc sort data=consec1; by id dt; run;&lt;/P&gt;
&lt;P&gt;data dtrange; /*date ranges of 3 consecutive dates*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set consec1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id dt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; mindt=lagdt2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; maxdt=dt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; keep id mindt maxdt;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data consec3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; merge have1 dtrange;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if mindt&amp;lt;=dt&amp;lt;=maxdt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; keep id dt amount;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;
&lt;P&gt;&amp;nbsp; select id, dt, amount, sum(amount) as consec3_sum&lt;/P&gt;
&lt;P&gt;&amp;nbsp; from consec3&lt;/P&gt;
&lt;P&gt;&amp;nbsp; group by id;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*Comments: This works for 3 consecutive days. I don't think there is an easy way to do for x number of consecutive days since the lag for each would be different*/&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 22:05:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885374#M349847</guid>
      <dc:creator>Seadrago</dc:creator>
      <dc:date>2023-07-18T22:05:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885383#M349854</link>
      <description>&lt;P&gt;Why, in your &lt;STRIKE&gt;ninth&lt;/STRIKE&gt; eighth observation, do you have consecutive_amount=1925?&amp;nbsp; Shouldn't it be 2006 if you really want the total for &lt;EM&gt;&lt;STRONG&gt;all&lt;/STRONG&gt;&lt;/EM&gt; consecutive dates?&amp;nbsp; (the 4-date sum of 22, 987, 916, 81 for 24AUG2021 through 27AUG2021).&amp;nbsp; Assuming that is an error, then this will work (I use variable TOTAL to replicate your variable CONSECUTIVE_AMOUNT):&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 $ DT:date9. Amount Consecutive_Amount;
format DT date9.;
datalines;
A 09JUL2021 3600 3600
A 03AUG2021 456 489
A 04AUG2021 33 33
A 06AUG2021 235 335
A 07AUG2021 100 100
A 09AUG2021 86 86
A 12AUG2021 456 456
A 24AUG2021 22 1925
A 25AUG2021 987 1984
A 26AUG2021 916 997
A 27AUG2021 81 81
B 07AUG2021 554 992
B 08AUG2021 193 438
B 09AUG2021 245 245
run;
data want (drop=nxt_:);

  /* Read and increment TOTAL until a date gap or next id is upcoming */
  merge have   have (firstobs=2 keep=id dt rename=(id=nxt_id dt=nxt_dt));
  total+amount;

  /*Then reread the same observations, output, and decrement TOTAL */
  if id^=nxt_id or dt+1 ^= nxt_dt then do until (total=0);
    set have;
    output;
    total=total-amount;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 12:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885383#M349854</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-19T12:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885404#M349861</link>
      <description>&lt;P&gt;Hi, thanks for your reply.&lt;/P&gt;
&lt;P&gt;This is really close!&lt;/P&gt;
&lt;P&gt;In the 8th observation, where DT = 24AUG2021, the&amp;nbsp;&lt;SPAN&gt;consecutive_amount should be 1925 because I only want the total for 3 consecutive days.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Another way to phrase this problem would be to say: I want the total amount for the period of the next 3 days if and only if there exist 3 &lt;STRONG&gt;consecutive&lt;/STRONG&gt; days.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Also, it would be great to be able to change the number of consecutive days.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For example, if I want the total for 4 consecutive days, then the consecutive amount for the 8th observation, where&amp;nbsp;DT = 24AUG2021, will be 2006.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 06:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885404#M349861</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2023-07-19T06:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885405#M349862</link>
      <description>&lt;P&gt;A DOW-based solution which only needs a slight tweak has been posted here:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-based-on-sequential-dates/td-p/791870" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-based-on-sequential-dates/td-p/791870&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 06:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885405#M349862</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-19T06:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885433#M349878</link>
      <description>&lt;P&gt;If you're adding only up to 3 consecutive amounts, subject to gap or id change boundaries, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=nxt_:);
  merge have
        have (firstobs=2 keep=id dt amount rename=(id=nxt1_id dt=nxt1_dt amount=nxt1_amt))
        have (firstobs=3 keep=id dt amount rename=(id=nxt2_id dt=nxt2_dt amount=nxt2_amt));

  if nxt1_id^=id or nxt1_dt^=dt+1 then total=amount;          else
  if nxt2_id^=id or nxt2_dt^=dt+2 then total=amount+nxt1_amt; else
  total=sum(amount,nxt1_amt,nxt2_amt);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jul 2023 12:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885433#M349878</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-19T12:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885451#M349883</link>
      <description>&lt;P&gt;I have done a few testing and I think that it works! Thanks a lot.&lt;/P&gt;
&lt;P&gt;It is also quite efficient in large volume of data.&lt;/P&gt;
&lt;P&gt;I will leave the post open for a day or so, because if you need to do the same piece of analysis for say 15 consecutive days then you will have to add the statements and calculations accordingly which is not very flexible.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 13:37:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885451#M349883</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2023-07-19T13:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Amount of consecutive days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885483#M349899</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11100"&gt;@Zatere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have done a few testing and I think that it works! Thanks a lot.&lt;/P&gt;
&lt;P&gt;It is also quite efficient in large volume of data.&lt;/P&gt;
&lt;P&gt;I will leave the post open for a day or so, because if you need to do the same piece of analysis for say 15 consecutive days then you will have to add the statements and calculations accordingly which is not very flexible.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you want maybe some other fixed limit than 3.&amp;nbsp; I already sent code to do accumulations over any number of consecutive days.&amp;nbsp; But if you still need a &lt;EM&gt;&lt;STRONG&gt;fixed&lt;/STRONG&gt;&lt;/EM&gt; upper limit on size-of-consecutive-sequence, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $ DT:date9. Amount Consecutive_Amount;
format DT date9.;
datalines;
A 09JUL2021 3600 3600
A 03AUG2021 456 489
A 04AUG2021 33 33
A 06AUG2021 235 335
A 07AUG2021 100 100
A 09AUG2021 86 86
A 12AUG2021 456 456
A 24AUG2021 22 1925
A 25AUG2021 987 1984
A 26AUG2021 916 997
A 27AUG2021 81 81
B 07AUG2021 554 992
B 08AUG2021 193 438
B 09AUG2021 245 245
run;
data want (drop=group_size nxt_:);

  /* Read and increment TOTAL until a date gap or next id is upcoming or group_size limit */
  merge have   have (firstobs=2 keep=id dt rename=(id=nxt_id dt=nxt_dt));
  total+amount;
  group_size+1;
  /*Then reread the same observations, output, and decrement TOTAL */
  if id^=nxt_id or dt+1 ^= nxt_dt or group_size=3 then do until (group_size=0);
    set have;
    output;
    total=total-amount;
    group_size=group_size-1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All you have to do is change the number 3 in the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if id^=nxt_id or dt+1 ^= nxt_dt or group_size=3 then do until (group_size=0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement to whatever fixed upper size limit you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this program passes through each group twice (where group "size" can be from 1 to 3 observations in your original specification).&amp;nbsp; The first pass builds the group total and looks for gaps or fixed group size limits.&amp;nbsp; The second re-reads the data, outputs it, and decrements the total.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 16:52:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Amount-of-consecutive-days/m-p/885483#M349899</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-19T16:52:05Z</dc:date>
    </item>
  </channel>
</rss>

