<?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: Calculating average in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/255036#M48699</link>
    <description>&lt;P&gt;If you have SAS/ETS software, you can &lt;A href="http://blogs.sas.com/content/iml/2016/01/27/moving-average-in-sas.html" target="_self"&gt;compute a three-month moving average by using PROC EXPAND &lt;/A&gt;and then merge the data and the average.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, the usual way to compute a three-month rolling average is to associate the rolling mean with the CURRENT month and the two PREVIOUS months. To use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;'s notation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; New_app&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;mean&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;lag1&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;lag2&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Mar 2016 18:08:29 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2016-03-07T18:08:29Z</dc:date>
    <item>
      <title>Calculating average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254836#M48642</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Need help in caliculating average of last &amp;nbsp;months from the current month, sample data is as below, pls help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; New &amp;nbsp; Return&amp;nbsp;Rework &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; New_app&lt;/P&gt;&lt;P&gt;01Mar2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100 &amp;nbsp; &amp;nbsp; 70 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&amp;nbsp;&lt;/P&gt;&lt;P&gt;01Apr2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 125 &amp;nbsp; &amp;nbsp; 80 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&amp;nbsp;&lt;/P&gt;&lt;P&gt;01May2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;150 &amp;nbsp; &amp;nbsp; 90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp;&lt;/P&gt;&lt;P&gt;01Jun2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 175 &amp;nbsp; &amp;nbsp;100 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;average(01mar2014,01apr2014,01may2014)=125 {average of last 3 months)&lt;/P&gt;&lt;P&gt;01Jul2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 200 &amp;nbsp; &amp;nbsp;110 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 70 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;average(01apr2014,01may2014,01jun2014)=150&lt;/P&gt;&lt;P&gt;01Aug2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 225 &amp;nbsp; &amp;nbsp;120 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 80 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;average(01may2014,01jun2014,01jul2015)=175&lt;/P&gt;&lt;P&gt;01Sep2014 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 250 &amp;nbsp; &amp;nbsp;130 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;average(01jun2014,01jul2015,01aug2015)=200&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Mar 2016 13:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254836#M48642</guid>
      <dc:creator>arjinram</dc:creator>
      <dc:date>2016-03-06T13:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254838#M48643</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/69245"&gt;@arjinram﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this could be a start:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create test data */
data have;
input Date :date. New Return Rework;
format date date9.;
cards;
01Mar2014 100 70 30 
01Apr2014 125 80 40 
01May2014 150 90 50 
01Jun2014 175 100 60 
01Jul2014 200 110 70 
01Aug2014 225 120 80 
01Sep2014 250 130 90 
;

proc sort data=have;
by date;
run; /* This requires SAS date values in variable DATE! */

/* Create variable New_app */
data want;
set have;
array v[3] _temporary_;
if n(of v[*])=3 then New_app=mean(of v[*]);
v[mod(_n_,3)+1]=new;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Probably, we will need to adapt this code to the requirements of your real data, if these may&amp;nbsp;involve, for example,&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;BY groups&lt;/LI&gt;
&lt;LI&gt;gaps &amp;gt; 1 month between two successive dates&lt;/LI&gt;
&lt;LI&gt;missing values of&amp;nbsp;variable NEW&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The above code simply computes the average of the previous three values of&amp;nbsp;variable NEW and ignores variable DATE for this calculation.&lt;/P&gt;</description>
      <pubDate>Sun, 06 Mar 2016 14:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254838#M48643</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-06T14:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254844#M48644</link>
      <description>&lt;P&gt;Here is another approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*, (select case when count(new)=3 then mean(new) else . end
             from have 
             where 1&amp;lt;=intck('month', date, a.date)&amp;lt;=3)
            as New_app
from have a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Unlike&amp;nbsp;my previous suggestion, this approach computes the average based on the date values: Those values of variable NEW are included in the calculation whose corresponding DATE values are 1, 2 or 3&amp;nbsp;months prior to the DATE value of the current observation. If this calculation involves fewer&amp;nbsp;than 3 non-missing values, New_app will be&amp;nbsp;left missing. (This is the case for the first three observations.) It is not anticipated that&amp;nbsp;more than 3&amp;nbsp;observations&lt;SPAN&gt;&amp;nbsp;could satisfy the date condition.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Mar 2016 15:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/254844#M48644</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-06T15:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/255001#M48689</link>
      <description>&lt;P&gt;And using &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;'s sorted Have set, here is another approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set have;
   New_app= mean(lag1(new),lag2(new),lag3(new));
   if _n_&amp;lt;4 then new_app=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Mar 2016 16:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/255001#M48689</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-07T16:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/255036#M48699</link>
      <description>&lt;P&gt;If you have SAS/ETS software, you can &lt;A href="http://blogs.sas.com/content/iml/2016/01/27/moving-average-in-sas.html" target="_self"&gt;compute a three-month moving average by using PROC EXPAND &lt;/A&gt;and then merge the data and the average.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, the usual way to compute a three-month rolling average is to associate the rolling mean with the CURRENT month and the two PREVIOUS months. To use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;'s notation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; New_app&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;mean&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;lag1&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;lag2&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 18:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-average/m-p/255036#M48699</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-03-07T18:08:29Z</dc:date>
    </item>
  </channel>
</rss>

