<?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: re: Cumulative Sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744027#M233029</link>
    <description>&lt;P&gt;Do a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by group1 date;
run;

data want;
do until (last.group1);
  set have;
  by group1;
  cumulative_revenue + revenue;
end;
do until (last.group1);
  set have;
  by group1;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 May 2021 19:55:26 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-05-26T19:55:26Z</dc:date>
    <item>
      <title>re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744008#M233020</link>
      <description>&lt;P&gt;Hi...I am trying to create a new variable that cumulates the total revenue. Because revenue was received on different dates for the same group1, I found the total revenue for each group and now I would like to cumulate the revenue after group1. Not sure if this the best approach.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Have;
	input group1 $ date :date9. revenue;
	format date date9.;
	datalines;
A 01jan2020 100
B 01jan2020 50
A 02jan2020 120
B 02jan2020 60
A 03jan2020 80
B 03jan2020 60
A 04jan2020 50
B 04jan2020 100
;
run;
 
proc sort data=Have;
	by group1 date;
run;

proc sql noprint;
	create table Have1 as 
		select *,
		sum(revenue) as revenue1
		from work.Have
		group by group1;
quit;

data Want;
	set Have1;
	by group1 revenue1;
	retain cumulative_revenue;
	if first.revenue1 then cumulative_revenue = revenue1;
run;

Want:

group1   date      revenue  cumulative_revenue
A     01jan2020      100         350
A     02jan2020      120         350
A     03jan2020       80         350
A     04jan2020       50         350
B     01jan2020       50         620
B     02jan2020       60         620
B     03jan2020       60         620
B     04jan2020      100         620&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 May 2021 19:15:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744008#M233020</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-05-26T19:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744027#M233029</link>
      <description>&lt;P&gt;Do a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by group1 date;
run;

data want;
do until (last.group1);
  set have;
  by group1;
  cumulative_revenue + revenue;
end;
do until (last.group1);
  set have;
  by group1;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 May 2021 19:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744027#M233029</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-26T19:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744031#M233030</link>
      <description>&lt;P&gt;Thanks Kurt!!!. It worked.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 20:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744031#M233030</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-05-26T20:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744032#M233031</link>
      <description>&lt;P&gt;Well, if it's giving you the correct results, I'd say that's a pretty good way to do it.&amp;nbsp; Now, if you need to do this on huge datasets and you encounter performance problems, then maybe the style of coding needs another look, but otherwise I think it's fine.&amp;nbsp; Correct results are 95% of the deal.&amp;nbsp; Making code "best" is secondary -- unless&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It's difficult for others to understand&lt;/LI&gt;
&lt;LI&gt;It's difficult to maintain&lt;/LI&gt;
&lt;LI&gt;It's "touchy," i.e. tends to fail a lot or frequently comes up with incorrect data.&amp;nbsp; You generally want robust code.&lt;/LI&gt;
&lt;LI&gt;Has significant performance problems&lt;/LI&gt;
&lt;LI&gt;Uses excess resources (disk hog, CPU hog, etc.) such that others are adversely affected.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Just my perspective,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 20:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744032#M233031</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-05-26T20:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744033#M233032</link>
      <description>&lt;P&gt;If the code is not entirely clear to you, feel free to ask.&lt;/P&gt;</description>
      <pubDate>Wed, 26 May 2021 20:07:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744033#M233032</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-26T20:07:56Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744160#M233091</link>
      <description>&lt;P&gt;Hi Jim....thanks for your comments. I am actually working with a large dataset. I did try Kurt's code and it seems to have worked for at least the few records that I have checked. But if there is a much robust approach and method that can be used I would certainly would like to try it. Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 27 May 2021 14:28:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744160#M233091</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2021-05-27T14:28:35Z</dc:date>
    </item>
    <item>
      <title>Re: re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744318#M233187</link>
      <description>Kurt's code looks pretty good.  Any time you are able to process a big dataset in a single pass, that's fastest way to go.  If the dataset is truly big, say more than 500 million rows, the are ways to do parallel processing which can really speed things up.  &lt;BR /&gt;&lt;BR /&gt;If you can't get the performance you need, and you read truly large datasets on a regular basis, then I could help you set that up, but it is a pretty advanced programming technique.</description>
      <pubDate>Fri, 28 May 2021 00:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/re-Cumulative-Sum/m-p/744318#M233187</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-05-28T00:21:57Z</dc:date>
    </item>
  </channel>
</rss>

