<?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: Sum by Group data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458061#M116217</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
center='A';lvalue=100;output;
center='A';lvalue=200;output;
center='A';lvalue=300;output;
center='A';lvalue=450;output;
center='B';lvalue=200;output;
center='B';lvalue=250;output;
center='B';lvalue=350;output;
center='C';lvalue=50;output;
center='C';lvalue=150;output;
center='C';lvalue=40;output;
center='C';lvalue=65;output;
center='C';lvalue=70;output;
run;
proc sort data=have;by center;run;
data want;
   set have;
   by center;
   if first.center then cumulative_sum=0;
   retain cumulative_sum 0;
   cumulative_sum=cumulative_sum+lvalue;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 27 Apr 2018 09:37:22 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2018-04-27T09:37:22Z</dc:date>
    <item>
      <title>Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458047#M116209</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data in the form of the following:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HAVE.jpg" style="width: 173px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20206i8456126A79D4B803/image-size/large?v=v2&amp;amp;px=999" role="button" title="HAVE.jpg" alt="HAVE.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I need to calculate the cumulative sum per Centre value like the following&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HAVE1.jpg" style="width: 284px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20207i64BECFA43472D524/image-size/large?v=v2&amp;amp;px=999" role="button" title="HAVE1.jpg" alt="HAVE1.jpg" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried writing a code for the same but it is taking the cumulative sum of all the rows without considering the Centre Value.&lt;/P&gt;&lt;P&gt;Please suggest a solution.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 09:13:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458047#M116209</guid>
      <dc:creator>sklal</dc:creator>
      <dc:date>2018-04-27T09:13:15Z</dc:date>
    </item>
    <item>
      <title>Re: Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458056#M116216</link>
      <description>&lt;P&gt;You can use the Search bar, there are loads of examples.&amp;nbsp; For instance:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain cum_sum;
  by center;
  cum_sum=ifn(_n_=1,lvalue,sum(cum_sum,lvalue));
run;&lt;/PRE&gt;
&lt;P&gt;Also, post test data in the form of a datastep, not here to type that out just to test it.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 09:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458056#M116216</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-27T09:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458061#M116217</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
center='A';lvalue=100;output;
center='A';lvalue=200;output;
center='A';lvalue=300;output;
center='A';lvalue=450;output;
center='B';lvalue=200;output;
center='B';lvalue=250;output;
center='B';lvalue=350;output;
center='C';lvalue=50;output;
center='C';lvalue=150;output;
center='C';lvalue=40;output;
center='C';lvalue=65;output;
center='C';lvalue=70;output;
run;
proc sort data=have;by center;run;
data want;
   set have;
   by center;
   if first.center then cumulative_sum=0;
   retain cumulative_sum 0;
   cumulative_sum=cumulative_sum+lvalue;
   
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Apr 2018 09:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458061#M116217</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2018-04-27T09:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458067#M116221</link>
      <description>&lt;P&gt;Thanks for the suggestion, I'll keep that in mind. I tried the code that you have written but it is summing up all the rows without considering the Centre value.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 09:51:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458067#M116221</guid>
      <dc:creator>sklal</dc:creator>
      <dc:date>2018-04-27T09:51:00Z</dc:date>
    </item>
    <item>
      <title>Re: Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458074#M116223</link>
      <description>&lt;P&gt;Yep forgot to reset the counter:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain cum_sum;
  by center;&lt;BR /&gt;  if first.center then cum_sum=0;
  cum_sum=ifn(_n_=1,lvalue,sum(cum_sum,lvalue));
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Apr 2018 10:26:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458074#M116223</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-04-27T10:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: Sum by Group data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458307#M116279</link>
      <description>&lt;P&gt;I applied the code to a larger data set with the relevant variables but now I'm getting the cumulative sum for all the rows irrespective of the category variable Centre. What could be the possible reason for this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Apr 2018 04:35:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-by-Group-data-step/m-p/458307#M116279</guid>
      <dc:creator>sklal</dc:creator>
      <dc:date>2018-04-28T04:35:50Z</dc:date>
    </item>
  </channel>
</rss>

