<?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 Complicated Cumulative Sum in All Things Community</title>
    <link>https://communities.sas.com/t5/All-Things-Community/Complicated-Cumulative-Sum/m-p/326563#M2164</link>
    <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class="vote"&gt;&lt;DIV class="favoritecount"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;&lt;DIV class="post-text"&gt;&lt;P&gt;I have a tricky question about conditional sum in SAS. Actually, it is very complicated for me and therefore, I cannot explain it by words. Therefore I want to show an example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B
 5      3
 7      2
 8      6
 6      4
 9      5
 8      2
 3      1
 4      3&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, I have a datasheet that has two columns. First of all, I calculated the conditional cumulative sum of column A ( I can do it by myself-So no need help for that step):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B    CA
 5      3    5
 7      2    12
 8      6    18    
 6      4    8     ((12+8)-18)+6
 9      5    17   
 8      2    18
 3      1    10    (((17+8)-18)+3
 4      3    14    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So my condition value is 18. If the cumulative more than 18, then it equal 18 and next value if sum of the first value after 18 and exceeds amount over 18. ( As I said I can do it by myself ) So the tricky part is I have to calculate the cumulative sum of column B according to column A:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B    CA    CB
 5      3    5     3
 7      2    12    5
 8      6    18    9.5 (5+(6*((18-12)/8)))
 6      4    8     5.5 ((5+6)-9.5)+4
 9      5    17    10.5 (5.5+5)
 8      2    18    10.75 (10.5+(2*((18-7)/8)))
 3      1    10    2.75 ((10.5+2)-10.75)+1
 4      3    14    5.75 (2.75+3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see from example the cumulative sum of column B is very specific. When column A is equal our condition value (18), then we calculate the proportion of the last value for getting our condition value (18) and then use this proportion for computing cumulative sum of column B.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Sun, 22 Jan 2017 10:53:51 GMT</pubDate>
    <dc:creator>Khaladdin</dc:creator>
    <dc:date>2017-01-22T10:53:51Z</dc:date>
    <item>
      <title>Complicated Cumulative Sum</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Complicated-Cumulative-Sum/m-p/326563#M2164</link>
      <description>&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class="vote"&gt;&lt;DIV class="favoritecount"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;&lt;DIV&gt;&lt;DIV class="post-text"&gt;&lt;P&gt;I have a tricky question about conditional sum in SAS. Actually, it is very complicated for me and therefore, I cannot explain it by words. Therefore I want to show an example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B
 5      3
 7      2
 8      6
 6      4
 9      5
 8      2
 3      1
 4      3&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see, I have a datasheet that has two columns. First of all, I calculated the conditional cumulative sum of column A ( I can do it by myself-So no need help for that step):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B    CA
 5      3    5
 7      2    12
 8      6    18    
 6      4    8     ((12+8)-18)+6
 9      5    17   
 8      2    18
 3      1    10    (((17+8)-18)+3
 4      3    14    &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So my condition value is 18. If the cumulative more than 18, then it equal 18 and next value if sum of the first value after 18 and exceeds amount over 18. ( As I said I can do it by myself ) So the tricky part is I have to calculate the cumulative sum of column B according to column A:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; A      B    CA    CB
 5      3    5     3
 7      2    12    5
 8      6    18    9.5 (5+(6*((18-12)/8)))
 6      4    8     5.5 ((5+6)-9.5)+4
 9      5    17    10.5 (5.5+5)
 8      2    18    10.75 (10.5+(2*((18-7)/8)))
 3      1    10    2.75 ((10.5+2)-10.75)+1
 4      3    14    5.75 (2.75+3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As you can see from example the cumulative sum of column B is very specific. When column A is equal our condition value (18), then we calculate the proportion of the last value for getting our condition value (18) and then use this proportion for computing cumulative sum of column B.&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 22 Jan 2017 10:53:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Complicated-Cumulative-Sum/m-p/326563#M2164</guid>
      <dc:creator>Khaladdin</dc:creator>
      <dc:date>2017-01-22T10:53:51Z</dc:date>
    </item>
    <item>
      <title>Re: Complicated Cumulative Sum</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Complicated-Cumulative-Sum/m-p/326594#M2165</link>
      <description>&lt;P&gt;The core of the solution is using RETAIN:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a tested code. If you are satisfied unmark the KEEP statement .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input a b;
datalines;
5 3
7 2
8 6
6 4
9 5
8 2
3 1
4 3
; run;&lt;BR /&gt;
data want;
 set have;
     &lt;STRONG&gt;retain&lt;/STRONG&gt; ca cb lag_a prev_ca alt_cb 0 prop 1 flag 0;
     lag_a = lag(a);
     
   if flag = 0 then do;  
     prev_ca = ca;
     if ca + a &amp;gt; 18 then ca=18;
     else ca = ca+a;
     
     if ca ge 18 then do;
        alt_cb = cb + b;     
        cb=cb+b*(18-prev_ca)/a;
        prop = cb;
        flag = 1;
     end;
     else  cb = cb + b;
   end; 
   else do;  /* flag=1 */
     ca= lag_a;
     cb = alt_cb - prop + b;
     flag = 0;
   end; 

   *keep a b ca cb ;  /* unmark if satisfied with results */
run; 



   &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 22 Jan 2017 17:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Complicated-Cumulative-Sum/m-p/326594#M2165</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-01-22T17:54:59Z</dc:date>
    </item>
  </channel>
</rss>

