<?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 sum (row operation) in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338642#M22493</link>
    <description>&lt;P&gt;I would use a datastep like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data A;
  input Col_1-Col_4;
  cards;
3      2        5       7
4      1        0       6
11     8        3       1
;
data B;
  input Col_1-Col_4;
  cards;
10      6        1       3
2       0        8       3
1       2        9       4
;

data want (keep=col:);
  set a;
  set b (rename=(Col_1-Col_4=_Col_1-_Col_4));
  array cols(*) Col_1-Col_4;
  array _cols(*) _Col_1-_Col_4;
  do i=1 to dim(cols);
    if i=1 then cols(i)=sum(cols(i),_cols(i));
    else do;
      cols(i)=sum(cols(i-1),cols(i),_cols(i));
    end;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Tue, 07 Mar 2017 01:55:53 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-03-07T01:55:53Z</dc:date>
    <item>
      <title>Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338636#M22490</link>
      <description>&lt;P&gt;So I have two datasets:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* dataset A */
          Col_1   Col_2   Col_3   Col_4
Row_1       3      2        5       7
Row_2       4      1        0       6
Row_3       11     8        3       1

/* dataset B */
           Col_1   Col_2   Col_3   Col_4
Row_1       10      6        1       3
Row_2       2       0        8       3
Row_3       1       2        9       4&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The final dataset that I need would look like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* dataset output */
           Col_1   Col_2    Col_3   Col_4
Row_1       13       21      27       37
Row_2       6        7       15       24
Row_3       12       22      34       39&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Bascially 1st&amp;nbsp;sum up the same cells from dataset A and dataset B.&lt;/P&gt;&lt;P&gt;then calculate the cumulative sum starting from Col_1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g. the Col_3, Row_2 cell value in output dataset would be calculated as: (4+2) + (0+1) + (0+8) = 15;&lt;/P&gt;&lt;P&gt;the Col_2, Row_1 cell: (10+3)+(2+6) = 21.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is quite easy to do in Excel, not sure how to do it in SAS?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 01:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338636#M22490</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-07T01:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338639#M22491</link>
      <description>&lt;P&gt;EG Tasks -&amp;gt; Append two datasets into 1, then use a Summary Task to sum the records across groups.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 01:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338639#M22491</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T01:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338642#M22493</link>
      <description>&lt;P&gt;I would use a datastep like the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data A;
  input Col_1-Col_4;
  cards;
3      2        5       7
4      1        0       6
11     8        3       1
;
data B;
  input Col_1-Col_4;
  cards;
10      6        1       3
2       0        8       3
1       2        9       4
;

data want (keep=col:);
  set a;
  set b (rename=(Col_1-Col_4=_Col_1-_Col_4));
  array cols(*) Col_1-Col_4;
  array _cols(*) _Col_1-_Col_4;
  do i=1 to dim(cols);
    if i=1 then cols(i)=sum(cols(i),_cols(i));
    else do;
      cols(i)=sum(cols(i-1),cols(i),_cols(i));
    end;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 01:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338642#M22493</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-07T01:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338654#M22494</link>
      <description>Hey thanks for the quick reply. Your codes worked.&lt;BR /&gt;&lt;BR /&gt;What if the name starts with 0, so column names are like 'Col_0', 'Col_1' to 'Col_3'? I did a little bit adjustments and replace '1','4' with '0','3' in your codes, but then it says: "ERROR: Array subscript out of range at line 44 column 29."&lt;BR /&gt;&lt;BR /&gt;I guess it has something to do with 0 but could you please tell me how to change it?</description>
      <pubDate>Tue, 07 Mar 2017 02:34:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338654#M22494</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-07T02:34:50Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338656#M22495</link>
      <description>&lt;P&gt;It worked fine for me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  input Col_0-Col_3;
  cards;
3      2        5       7
4      1        0       6
11     8        3       1
;
data B;
  input Col_0-Col_3;
  cards;
10      6        1       3
2       0        8       3
1       2        9       4
;

data want (keep=col:);
  set a;
  set b (rename=(Col_0-Col_3=_Col_0-_Col_3));
  array cols(*) Col_0-Col_3;
  array _cols(*) _Col_0-_Col_3;
  do i=1 to dim(cols);
    if i=1 then cols(i)=sum(cols(i),_cols(i));
    else do;
      cols(i)=sum(cols(i-1),cols(i),_cols(i));
    end;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338656#M22495</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T02:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338661#M22496</link>
      <description>&lt;P&gt;My guess would be that in changing the number you also changed, but should'nt have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;do i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; to &lt;SPAN class="token function"&gt;dim&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;cols&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That, and the statements under it, shouldn't be changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 02:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338661#M22496</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-07T02:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338678#M22497</link>
      <description>Hi, in addition to this problem, what if there are more than two input datasets? like three, four datasets. (and users will specify an input, a number, to determine how many datasets they will use)&lt;BR /&gt;&lt;BR /&gt;Then how should we calculate the sum? (rather than hard-code). Suppose datasets have name like: Lib.&amp;amp;type._&amp;amp;class._&amp;amp;j._name; where 'type', 'class', 'j' are global macro variables.</description>
      <pubDate>Tue, 07 Mar 2017 05:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/338678#M22497</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-07T05:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339055#M22511</link>
      <description>&lt;P&gt;Just figured out a solution:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sum;
data want (keep=Class Array:);      
     retain Class Array:;
     array Array(*) Array_0-Array_%eval(&amp;amp;num.-1);
     do i=1 to dim(Array);
     Array(i)=0;            /* give the array initial values */
     end;

     %do _l=1 %to &amp;amp;Num_type;
     %let Type_name=%scan(&amp;amp;type,&amp;amp;_l);
        set lib.&amp;amp;type;
        array &amp;amp;Type_name.(*) &amp;amp;Type_name._0-&amp;amp;Type_name._%eval(&amp;amp;num.-1);    /* rename each column */
        do i=1 to dim(Array);
          Array(i)=&amp;amp;Type_name.(i) + Array(i);        /* sum up */
        end;
     %end;

run;
%mend sum;
%sum;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This process can be further expanded to cover more macro variables.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 23:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339055#M22511</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-07T23:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339058#M22512</link>
      <description>&lt;P&gt;Macros are one way, but I would suggest IML or Base SAS instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a way, doesn't matter how many datasets or variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
	input Col_1-Col_4;
	cards;
3      2        5       7
4      1        0       6
11     8        3       1
;

data B;
	input Col_1-Col_4;
	cards;
10      6        1       3
2       0        8       3
1       2        9       4
;

data combined;
	set a b indsname=source;;

	*doesn't matter how many datasets here;
	dsn=source;
run;

proc sort data=combined;
	by dsn;
run;

data combined;
	set combined;
	by dsn;

	if first.dsn then
		row_count=1;
	else row_count+1;
run;

proc means data=combined nway noprint;
	class row_count;
	var col_1-col_4;

	*list all vars here, doesn't matter how many;
	output out=want (drop = _type_ _freq_) sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2017 23:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339058#M22512</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T23:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum (row operation)</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339062#M22513</link>
      <description>&lt;P&gt;I have no idea how you would do something like that in Excel.&lt;/P&gt;
&lt;P&gt;But it is easy in SAS.&lt;/P&gt;
&lt;P&gt;First combine all of the datasets and rotate into tall skinny format. &amp;nbsp;Note you could make this step as a view if you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combine;
 set  B A indsname=source ;
 array c _numeric_;
 if source ne lag(source) then row=0;
 row+1;
 do col=1 to dim(c);
   value=c(col);
   output;
 end;
 keep row col value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then calculate the sum of the values for the same ROW*COL cell;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary nway ;
  class row col ;
  var value ;
  output out=sum (keep=row col value) sum=value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then generate the cummulative sums across the columns.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cum_sum ;
  do until (last.row);
    set sum ;
    by row col;
    new_value = sum(new_value,value);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then if you want you could convert it back into a wide format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=cum_sum out=want(drop=_: ) prefix=col_ ;
  by row ;
  id col ;
  var new_value ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Mar 2017 00:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Cumulative-sum-row-operation/m-p/339062#M22513</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-03-08T00:12:04Z</dc:date>
    </item>
  </channel>
</rss>

