<?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 in the order of rank variable by byvar list in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776052#M246735</link>
    <description>&lt;P&gt;Hi Ksharp,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a self-correlated subquery, this problem is solved by an elegant SQL way. I was thinking if this could be done by Group by statement. Probably not, as sql does not provide cusum summary statistic. I was also wondering if this could be generalized to a macro when one does not know in advance the number of factors. Say,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let byvar_list=factor1 factor2;
%macro cusum_byvar(indata=temp, byvar=&amp;amp;byvar_list, outdata=);

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks for your sql solution.&lt;/P&gt;</description>
    <pubDate>Sat, 23 Oct 2021 19:26:39 GMT</pubDate>
    <dc:creator>Macro</dc:creator>
    <dc:date>2021-10-23T19:26:39Z</dc:date>
    <item>
      <title>Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775553#M246528</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create a new dataset from an old dataset, so that the new dataset will have cumulative sum of variables from the old dataset. This should be done by byvar variables in the old dataset.&amp;nbsp; An old data example is like this temp dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input Factor1 $  Factor2 $  rank x y;
datalines;
A  B  1  1  2
A  B  2  2  4
A  B  3  3  6
A  C  1  1  7
A  C  2  3  8
A  C  3  5  9
;
run
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The new dataset should be like this temp2 dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp2;
input Factor1 $  Factor2 $  rank x y csum_x  csum_y;
datalines;
A  B  1  1  2  1  2
A  B  2  2  4  3  6
A  B  3  3  6  6  12
A  C  1  1  7  1  7
A  C  2  3  8  4  15
A  C  3  5  9  9  24
;
run&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The cumulative sum of x and y are done by Factor1 and Factor2 combination, and along with the rank variable rank. The general factor variable combination could be&amp;nbsp; more than 2 factor variables. Is there any data step or proc sql way to do this? Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 04:34:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775553#M246528</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-21T04:34:48Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775559#M246532</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
&amp;nbsp; set HAVE;
  by FACROR1 FACTOR2;
  if first.FACTOR2 then call missing(SUMX, SUMY);
  SUMX+X;
  SUMY+Y;
 run.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Oct 2021 05:34:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775559#M246532</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-21T05:34:19Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775614#M246559</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input Factor1 $  Factor2 $  rank x y;
datalines;
A  B  1  1  2
A  B  2  2  4
A  B  3  3  6
A  C  1  1  7
A  C  2  3  8
A  C  3  5  9
;
run;

proc sql;
create table temp2 as
select *,
(select sum(x) from temp where Factor1=a.Factor1 and Factor2=a.Factor2 and rank le a.rank) as csum_x,
(select sum(y) from temp where Factor1=a.Factor1 and Factor2=a.Factor2 and rank le a.rank) as csum_y
 from temp as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Oct 2021 11:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/775614#M246559</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-21T11:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776051#M246734</link>
      <description>&lt;P&gt;Hi Chris,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for this solution. It is good to know there is a missing routine that reset cusum operation in data step. More generally, it could be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.factor1 or first.factor2 then call missing(sumx, sumy);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to take care of the level change of the factor combination. You provided the simplest solution.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 19:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776051#M246734</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-23T19:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776052#M246735</link>
      <description>&lt;P&gt;Hi Ksharp,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using a self-correlated subquery, this problem is solved by an elegant SQL way. I was thinking if this could be done by Group by statement. Probably not, as sql does not provide cusum summary statistic. I was also wondering if this could be generalized to a macro when one does not know in advance the number of factors. Say,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let byvar_list=factor1 factor2;
%macro cusum_byvar(indata=temp, byvar=&amp;amp;byvar_list, outdata=);

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks for your sql solution.&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 19:26:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776052#M246735</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-23T19:26:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776056#M246737</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;first.factor1&lt;/FONT&gt;&amp;nbsp; &amp;nbsp;is always true if&amp;nbsp; &amp;nbsp;&lt;FONT face="courier new,courier"&gt;first.factor2&amp;nbsp;&lt;/FONT&gt; &amp;nbsp; is true&lt;/P&gt;</description>
      <pubDate>Sat, 23 Oct 2021 22:49:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776056#M246737</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-10-23T22:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776071#M246742</link>
      <description>Sure . of course, you can make a macro .</description>
      <pubDate>Sun, 24 Oct 2021 09:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776071#M246742</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-10-24T09:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum in the order of rank variable by byvar list</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776650#M247039</link>
      <description>&lt;P&gt;I guess you meant the reverse of the statement is true.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Oct 2021 02:07:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-in-the-order-of-rank-variable-by-byvar-list/m-p/776650#M247039</guid>
      <dc:creator>Macro</dc:creator>
      <dc:date>2021-10-27T02:07:14Z</dc:date>
    </item>
  </channel>
</rss>

