<?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 every X group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835955#M330525</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/342595"&gt;@fierceanalytics&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a start, try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
month='01JAN2016'd;
do keyx=1 to 27;
  month=intnx('month',month,rand('integer',4));
  dollar=round(rand('uniform',1,9),0.1);
  output;
end;
format month yymmd7. dollar dollar12.2;
run;

%let X=3;

/* Create sums of DOLLAR for groups of X consecutive observations */

data want(keep=range sum);
length range $7;
range=cat(put(&amp;amp;X*(_n_-1)+1,2.),' - ',put(&amp;amp;X*_n_,2.));
do i=1 to &amp;amp;X;
  set have;
  sum=sum(sum,dollar);
end;
format sum dollar12.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(But I wrote this &lt;EM&gt;before&lt;/EM&gt; you requested an "elegant idea.")&lt;/P&gt;</description>
    <pubDate>Thu, 29 Sep 2022 20:56:09 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-09-29T20:56:09Z</dc:date>
    <item>
      <title>sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835935#M330515</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To illustrate,&amp;nbsp; data set has 27 records,&amp;nbsp; two variables, Month and Dollar. Month has 27 nonmissing month values. Dollar is a numeric. Sorted the data set by Month ascendingly. Do not care if months are consecutive or not, but physical order from lower to higher is important. So after sorting, create a new order: keyx=_n_;. Now keyx= has 1.... 27, the order is verified correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to sum Dollar by 1-3, 4-6,.... 25-27, three entries each? In other words, result will 9 sums, instead of 27, kind of skip sum by X group?&amp;nbsp; Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 19:53:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835935#M330515</guid>
      <dc:creator>fierceanalytics</dc:creator>
      <dc:date>2022-09-29T19:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835951#M330522</link>
      <description>&lt;P&gt;It would be a good idea to provide an example of your data in the form of a data step .&lt;/P&gt;
&lt;P&gt;And exactly what the final data set would look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any time that "Month" or other date, time or datetime information is mentioned there may be opportunities to use SAS supplied tools for dealing with them. But an actual example of the values involved is good idea to provide best or sometimes even just good, practices.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 20:27:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835951#M330522</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-29T20:27:14Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835954#M330524</link>
      <description>&lt;P&gt;Let me further simply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Go by _n_ order (forget about that Month field). When _n_ =1 to 3, first sum of dollar field. when _n_=4 to 6, second sum.&amp;nbsp; how to order _n_ using month or other field is not the subject here. There are 27 rows originally. So look for 9 sums.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can use 'clumsy ways' to get it done. like to hear some elegant idea. Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 20:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835954#M330524</guid>
      <dc:creator>fierceanalytics</dc:creator>
      <dc:date>2022-09-29T20:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835955#M330525</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/342595"&gt;@fierceanalytics&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a start, try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
month='01JAN2016'd;
do keyx=1 to 27;
  month=intnx('month',month,rand('integer',4));
  dollar=round(rand('uniform',1,9),0.1);
  output;
end;
format month yymmd7. dollar dollar12.2;
run;

%let X=3;

/* Create sums of DOLLAR for groups of X consecutive observations */

data want(keep=range sum);
length range $7;
range=cat(put(&amp;amp;X*(_n_-1)+1,2.),' - ',put(&amp;amp;X*_n_,2.));
do i=1 to &amp;amp;X;
  set have;
  sum=sum(sum,dollar);
end;
format sum dollar12.2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(But I wrote this &lt;EM&gt;before&lt;/EM&gt; you requested an "elegant idea.")&lt;/P&gt;</description>
      <pubDate>Thu, 29 Sep 2022 20:56:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835955#M330525</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-09-29T20:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835959#M330527</link>
      <description>Elegance is in the eye of the beholder.  Here is one approach once your data set is sorted.&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;groupnum + 1;&lt;BR /&gt;sum = 0;&lt;BR /&gt;do i=1 to 3;&lt;BR /&gt;   set have;&lt;BR /&gt;   sum + dollars;&lt;BR /&gt;end;&lt;BR /&gt;keep groupnum sum;&lt;BR /&gt;run;</description>
      <pubDate>Thu, 29 Sep 2022 22:00:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835959#M330527</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-09-29T22:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835964#M330529</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/342595"&gt;@fierceanalytics&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Let me further simply.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Go by _n_ order (forget about that Month field). When _n_ =1 to 3, first sum of dollar field. when _n_=4 to 6, second sum.&amp;nbsp; how to order _n_ using month or other field is not the subject here. There are 27 rows originally. So look for 9 sums.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can use 'clumsy ways' to get it done. like to hear some elegant idea. Thank you.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Try this using a data set I create with exactly 27 values.&lt;/P&gt;
&lt;PRE&gt;data junk;
  do i= 1 to 27;
     value= rand('integer',20);
     output;
   end;
run;

data want;
   set junk;
   total=sum(lag(value),lag2(value),lag3(value));
   if mod(_n_,3)=0;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Sep 2022 22:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835964#M330529</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-29T22:13:58Z</dc:date>
    </item>
    <item>
      <title>Re: sum by every X group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835976#M330532</link>
      <description>Thanks. Better than mine.</description>
      <pubDate>Thu, 29 Sep 2022 23:53:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-by-every-X-group/m-p/835976#M330532</guid>
      <dc:creator>fierceanalytics</dc:creator>
      <dc:date>2022-09-29T23:53:35Z</dc:date>
    </item>
  </channel>
</rss>

