<?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: Group totals based on 3 month interval in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371051#M88634</link>
    <description>&lt;P&gt;Best way would be do by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;suggestion.&lt;/P&gt;
&lt;P&gt;But yu can achieve same by proc sql as shown below&lt;/P&gt;
&lt;PRE&gt;proc sql;
select *,
sum(dept1N) as dept1_3months_total, 
sum(dept2N) as dept2_3months_total,
calculated dept1_3months_total+ calculated dept2_3months_total as dept1_dept2total,
(calculated dept1_3months_total/calculated dept1_dept2total)*100 as mothavg
from 
(select 
min(month) as min_date format =date9. from test) a cross join
(select * from test)b
group by ceil((intck('month', min_date, month)+1)/3);
&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Jun 2017 21:00:37 GMT</pubDate>
    <dc:creator>kiranv_</dc:creator>
    <dc:date>2017-06-27T21:00:37Z</dc:date>
    <item>
      <title>Group totals based on 3 month interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371025#M88624</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; test;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;infile&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; Year $ Month $ Dept1N Dept2N ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;return&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;2014 1Jun2014 2500 2100&lt;/P&gt;
&lt;P&gt;2014 1Jul2014 2330 2220&lt;/P&gt;
&lt;P&gt;2014 1Aug2014 1500 2140&lt;/P&gt;
&lt;P&gt;2014 1Sep2014 2500 2670&lt;/P&gt;
&lt;P&gt;2014 1Oct2014 4500 4100&lt;/P&gt;
&lt;P&gt;2014 1Nov2014 2600 7100&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3"&gt;Output&lt;/FONT&gt;&lt;/P&gt;
&lt;TABLE width="256" style="width: 192pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" span="4" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" style="border: 0px windowtext; border-image: none; width: 48pt; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Year&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px windowtext; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Month&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px windowtext; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Dept1N&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px windowtext; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;Dept2N&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Jun2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2500&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2100&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Jul2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2330&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2220&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Aug2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1500&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2140&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Sep2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2500&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2670&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Oct2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4500&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4100&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px windowtext; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1Nov2014&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2600&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD align="right" style="border: 0px windowtext; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;7100&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I need to add a 3 month Avg column based on the following&lt;/P&gt;
&lt;P&gt;Dept1N(sum of the 1st 3 rows, then 2nd 3 rows...etc.&amp;nbsp; Example 2500+2330+1500=6330)&lt;/P&gt;
&lt;P&gt;Dept2N(2100+2220+2140=6460)&lt;/P&gt;
&lt;P&gt;Add Dept1N+Dept2N (6330+6460=12790&lt;/P&gt;
&lt;P&gt;To get my 3 month avg 6460/12790 = 50.50&lt;/P&gt;
&lt;P&gt;Can this be best handles in proc report or here in the datastep and how??&lt;/P&gt;
&lt;P&gt;Thus the calculation needs to evaluate every 3 months&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 18:43:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371025#M88624</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2017-06-27T18:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: Group totals based on 3 month interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371032#M88626</link>
      <description>&lt;P&gt;In a data step use the LAG function if you are sure you have no missing months. If you have missing months, a SQL query or PROC EXPAND are your best option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have missing months that you need to deal with?&lt;/P&gt;
&lt;P&gt;If no,then the following is probably the quickest method:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;average3 = currentVar + lag(currentVar) + lag2(currentVar);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: I suppose if this is an average you should divide by 3 or use the MEAN function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;average3 = mean(curentVar, lag1(currentVar), lag2(currentVar));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 19:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371032#M88626</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-27T19:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Group totals based on 3 month interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371050#M88633</link>
      <description>&lt;P&gt;If my understanding of your question is correct, this should get you 80% of the way there. I don't understand the examples you gave. You listed the 3 month sum for department 2&amp;nbsp;divided by the 3 month sum for both departments and called it average? I believe this calculation is the percentage of department 2's three-month sales. Anyway, here is the code to append the static 3 month averages to the base file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
infile datalines;
input Year $ Month $ Dept1N Dept2N ;
return;
datalines;
2014 1Jun2014 2500 2100
2014 1Jul2014 2330 2220
2014 1Aug2014 1500 2140
2014 1Sep2014 2500 2670
2014 1Oct2014 4500 4100
2014 1Nov2014 2600 7100
;
run;

*Create identifiers for each 3 month segment;
data want;
set test;
three_month_count=ceil(_n_/3);
run;

*Calculate static three month averages for each department;
proc summary data = want nway nmiss;
class three_month_count;
var Dept1N Dept2N;
output out = want2 ( drop = _type_ _freq_ ) mean = avg_Dept1N avg_Dept2N;
run;

*Merge the three month averages back to the base file;
data append_averages;
merge want want2;
by three_month_count;&lt;BR /&gt;*Add any calculations you need here;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2017 20:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371050#M88633</guid>
      <dc:creator>Rwon</dc:creator>
      <dc:date>2017-06-27T20:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Group totals based on 3 month interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371051#M88634</link>
      <description>&lt;P&gt;Best way would be do by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;suggestion.&lt;/P&gt;
&lt;P&gt;But yu can achieve same by proc sql as shown below&lt;/P&gt;
&lt;PRE&gt;proc sql;
select *,
sum(dept1N) as dept1_3months_total, 
sum(dept2N) as dept2_3months_total,
calculated dept1_3months_total+ calculated dept2_3months_total as dept1_dept2total,
(calculated dept1_3months_total/calculated dept1_dept2total)*100 as mothavg
from 
(select 
min(month) as min_date format =date9. from test) a cross join
(select * from test)b
group by ceil((intck('month', min_date, month)+1)/3);
&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2017 21:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Group-totals-based-on-3-month-interval/m-p/371051#M88634</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-27T21:00:37Z</dc:date>
    </item>
  </channel>
</rss>

