<?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: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693243#M211349</link>
    <description>&lt;P&gt;Thanks very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;. This is very helpful. Really appreciate the help&lt;/P&gt;</description>
    <pubDate>Wed, 21 Oct 2020 16:30:33 GMT</pubDate>
    <dc:creator>kashun</dc:creator>
    <dc:date>2020-10-21T16:30:33Z</dc:date>
    <item>
      <title>Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692672#M211042</link>
      <description>&lt;P&gt;I have been struggling a little with how I can create a cumulative sum of two variable s using do until (last.ID). The data shown below is what I have.&lt;/P&gt;&lt;P&gt;The logic I would like to use is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;If (amount + Accum_test_ID2)&amp;nbsp; &amp;gt;= Check2 then amount = (Check2 - Accum_test_ID2)&lt;BR /&gt;else if (amount + Accum_test_ID1) &amp;gt;= Check1 then amount = (Check1 -Accum_test_ID2)&lt;BR /&gt;else amount = amount&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the cumulative sum is dependent on ID1, ID2 and Date, I am finding it difficult to use sort by statement to create Accum_test_ID2 which for some reason messes up the cumulative sum for specific dates.&lt;/P&gt;&lt;P&gt;I tried using DO UNTIL(LAST.ID1); And in the do loop I grouped the data by ID1 and DATE and then retained Accum_test_D1 Accum_test_D2 0;&lt;/P&gt;&lt;P&gt;And also created cumulative sum: if first.ID then Accum_test_ID1 = test&lt;/P&gt;&lt;P&gt;else Accum_test_ID1+test.&lt;/P&gt;&lt;P&gt;Similarly if first.ID then Accum_test_ID2 = test else Accum_test_ID2+test.&lt;/P&gt;&lt;P&gt;With the above approach, I do get&amp;nbsp;Accum_test_ID2 the same as&amp;nbsp;Accum_test_ID1 since I grouped it by only D1 and Date.&lt;/P&gt;&lt;P&gt;I do want to know how to group it by both D1 and D2 without changing the date arrangement to correctly calculate the cumulative sums.&lt;/P&gt;&lt;P&gt;Note: test = sum(amount, small)&lt;/P&gt;&lt;P&gt;Accum_test_ID1 is the accumulative sum of test by ID1&lt;/P&gt;&lt;P&gt;Accum_TEST_ID2 is the accumulative sum of test by ID2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
test = 0;
do until (last.ID1);
set have;
by ID1 Date;
retain accum_test_ID1 accum_test_ID2  0;
if (amount +accum_test_ID2) &amp;gt; = Check2 then amount = (Check2 - accum_test_ID2);
else if (amount + accum_test_ID1) &amp;gt; = Check then amount = (Check1 - accum_test_ID2);
else amount =amount;
test = Sum(amount,Small);
if first.ID1 then accum_test_ID1 = test;
else accum_test_ID1+test;
if first.ID2 then accum_test_ID2 = test;
else accum_test_ID2 = test;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ID1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;ID2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;DATE&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Amount&lt;/P&g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P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1200&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;600&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;870&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1303&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;938&lt;/P&gt;&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;</description>
      <pubDate>Mon, 19 Oct 2020 19:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692672#M211042</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-19T19:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692674#M211043</link>
      <description>&lt;P&gt;You can only use First. with variables that are on your BY statement. Since Id2 is NOT on the by statement you cannot use First.Id2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is sorted in a grouped order you could use: By Id1 id2 date notsorted; to use the groupings and get the first and last.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a data step to provide data I'm not going to test anything at this time as I don't have time to write a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 19:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692674#M211043</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-19T19:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692831#M211146</link>
      <description>Thanks very much &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;. I tried using by id1 id2 date notsorted but the accum_test_id2 doesn't continue with the accumulation. It begins with the test value on every first.&lt;BR /&gt;Eg. I get ID2 Accum_test_ID2&lt;BR /&gt;0 1530&lt;BR /&gt;1 250&lt;BR /&gt;1 370&lt;BR /&gt;0 120&lt;BR /&gt;0 210&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 20 Oct 2020 12:21:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692831#M211146</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T12:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692953#M211206</link>
      <description>&lt;P&gt;In your sample WANT dataset, every value of TEST is simply the sum of amount+small, where amount has its original value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yet your program attempts to conditionally modify amount prior to the "test=sum(amount,small);" statement.&amp;nbsp; Given the desired resulting value of test that you show, I would ordinarily conclude that you never expect amount to be changed, but your desired value of amount is changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not understand the rules you are trying to implement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And, like &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; , I would appreciate it if you could provide a working data step generating the dataset HAVE from your sample data.&amp;nbsp; I'd rather spend my time helping you to solve the problem than making a working sample dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692953#M211206</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-10-20T16:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692958#M211211</link>
      <description>&lt;P&gt;Since TEST is effectively the sum of amount and small (until you provide data showing otherwise), I believe this program will generate WANT from HAVE in your sample.&amp;nbsp; Whether it fits your rules as stated, I can't say.&amp;nbsp; Test it on an actual sample data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id1 id2 notsorted;
  test=sum(small,amount);
  accum_test_id1+test;
  if first.id1 then accum_test_id1=test;

  accum_test_id2+test;
  if first.id2 then accum_test_id2=test;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And you probably don't need the intermediate variable &lt;STRONG&gt;test&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id1 id2 notsorted;

  accum_test_id1+small+amount;
  if first.id1 then accum_test_id1=sum(small,amount);

  accum_test_id2+small+amount;
  if first.id2 then accum_test_id2=sum(small,amount);
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>Tue, 20 Oct 2020 17:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/692958#M211211</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-10-20T17:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693016#M211248</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;. I think your code was close to what I wanted.. I used the data below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input @1 ID1 @6 ID2 @8 DATE DATE9. @18 Amount @24 small @27 limit1 @32 limit2;
FORMAT DATE date9.;
datalines;
1456 0 01JAN2020 1500  30 4000 2000 
1456 1 03MAR2020 200   50 4000 2000 
1456 1 04MAR2020 100   20 4000 2000
1456 0 09JUN2020 100   20 4000 2000
1456 0 12JUL2020 400   70 4000 2000
3314 0 04FEB2020 50    40 1200 600 
3314 0 08FEB2020 250   25 1200 600
3314 1 10MAR2020 20    48 1200 600
3314 1 12APR2020 800   70 1200 600
;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But get the result below when I use your program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Get:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Get.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50854iD3A982AA85D4DEA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="Get.png" alt="Get.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Want&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/50853i7331D2D40E6592AD/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.png" alt="want.png" /&gt;&lt;/span&gt;&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, 20 Oct 2020 20:34:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693016#M211248</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T20:34:17Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693065#M211270</link>
      <description>&lt;P&gt;OK, I get it now.&amp;nbsp; If an ID2 value recurs within an ID1 group, you want the accumulator to bridge any gap between occurences of that ID2 value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for setting up a real SAS data step.&amp;nbsp; I've tested the program this time.&amp;nbsp; It keeps a separate accumulator for ID2=0 and for ID2=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

input @1 ID1 @6 ID2 @8 DATE DATE9. @18 Amount @24 small @27 limit1 @32 limit2;
FORMAT DATE date9.;
datalines;
1456 0 01JAN2020 1500  30 4000 2000 
1456 1 03MAR2020 200   50 4000 2000 
1456 1 04MAR2020 100   20 4000 2000
1456 0 09JUN2020 100   20 4000 2000
1456 0 12JUL2020 400   70 4000 2000
3314 0 04FEB2020 50    40 1200 600 
3314 0 08FEB2020 250   25 1200 600
3314 1 10MAR2020 20    48 1200 600
3314 1 12APR2020 800   70 1200 600
;

RUN;

data want (drop=_:);
  set have;
  by id1;
  test=sum(small,amount);
  accum_test_id1+test;
  if first.id1 then do;
    accum_test_id1=test;
    _id2_accum0=0;
    _id2_accum1=0;
  end;

  if id2=0 then _id2_accum0+test; else
  if id2=1 then _id2_accum1+test;

  accum_test_id2=ifn(id2=0,_id2_accum0,_id2_accum1);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes 2 explicit values for ID2 - 0 or 1.&amp;nbsp; Of course if you have more values, and if you know in advance what they might be, you can just generate an accumulator variable for each value.&amp;nbsp; Of course, that gets annoying if you have more than a very few ID2 values.&lt;BR /&gt;&lt;BR /&gt;But if you don't know the ID2's in advance, or you have a lot of distinct ID2's, then a hash object to store the accumulator keyed on each encountered ID2 value will be the right solution, as below.&amp;nbsp; I didn't bother here to create the intermediate variable test, but that's not because I'm using a hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  by id1;

  if _n_=1 then do;
    declare hash h ();
	 h.definekey('id2');
	 h.definedata('accum_test_id2');
	 h.definedone();
  end;

  accum_test_id1+amount+small;
  if first.id1 then accum_test_id1=sum(amount,small);

  if h.find()=0 then accum_test_id2+amount+small;
  else accum_test_id2=sum(amount,small);
  h.replace();

  if last.id1 then h.clear();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Think of the hash object h as a lookup table, keyed on ID2 values.&amp;nbsp; The lookup table keeps an updated value for accum_test_id2 for each of those ID2's.&amp;nbsp; So when the lookup of the current value of ID2 is successful (i.e.&amp;nbsp; h.find()=0), then that value is retrieved by the find method, and immediately added to amount+small.&amp;nbsp; Otherwise the ID2 value is new, and the accumulator is directly calculated from amount and small.&amp;nbsp; Then the updated accumulator value is put (back) into the hash lookup awaiting the next instance for the current ID2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end of each ID1, the hash object is cleared to avoid corrupting the id2 accumulators for the next ID1 group.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2020 04:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693065#M211270</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-10-21T04:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Simultaneous Cumulative Sum  of two variables with respect to date using Do Until loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693243#M211349</link>
      <description>&lt;P&gt;Thanks very much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;. This is very helpful. Really appreciate the help&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2020 16:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simultaneous-Cumulative-Sum-of-two-variables-with-respect-to/m-p/693243#M211349</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-21T16:30:33Z</dc:date>
    </item>
  </channel>
</rss>

