<?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: Calculate a modified version of moving averages in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417675#M280384</link>
    <description>&lt;P&gt;Use the CONVERT statement and the&amp;nbsp;transformout=(cmovave 5) option in PROC EXPAND and do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=YourData;
	id Week;
	convert value=smoothed_average / transformout=(cmovave 5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;This creates a Centered Moving Average of five observations (The two preceding, the present and the two next observations)&lt;/P&gt;</description>
    <pubDate>Fri, 01 Dec 2017 09:59:43 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2017-12-01T09:59:43Z</dc:date>
    <item>
      <title>Calculate a modified version of moving averages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417672#M280383</link>
      <description>&lt;P&gt;I have a data set which looks like this.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.PNG" style="width: 196px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16952i314A886726B76FAB/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.PNG" alt="1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I want to calculate for every row, starting from row number three a moving average with window 5. For every row i, the window will have rows i-2, i-1, i, i+1, i+2. The desired output is like this.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2.PNG" style="width: 323px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/16953i73C71EF95EB088D4/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.PNG" alt="2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So for week 3, The smoothed_average is a mean of the highlighted cells.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 08:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417672#M280383</guid>
      <dc:creator>pakalu_papito</dc:creator>
      <dc:date>2017-12-01T08:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a modified version of moving averages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417675#M280384</link>
      <description>&lt;P&gt;Use the CONVERT statement and the&amp;nbsp;transformout=(cmovave 5) option in PROC EXPAND and do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=YourData;
	id Week;
	convert value=smoothed_average / transformout=(cmovave 5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;This creates a Centered Moving Average of five observations (The two preceding, the present and the two next observations)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 09:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417675#M280384</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-12-01T09:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a modified version of moving averages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417745#M280385</link>
      <description>&lt;P&gt;Next time write some code to make it as a table ,not picture. No one would like to type it for you ,if you want someone answer your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input week sales promo;
cards;
1 792 0
2 271 0
3 490 0
4 1095 0
5 1439 0
6 597 0
7 588 0
8 7078 0
;
run;
proc sql;
select * ,(select avg(sales) from have where week between a.week-2 and a.week+2) as avg
 from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Dec 2017 13:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417745#M280385</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-12-01T13:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a modified version of moving averages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417750#M280386</link>
      <description>&lt;P&gt;If u don't have SAS/ETS you won't be able to use proc expand. Here is a data step approach that should work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have nobs=nobs;
	if _N_ &amp;gt; 2 and _N_ &amp;lt;= nobs-2 then do; *Logic to only calculate a smoothed average when there are two prior rows as well as two following rows;
		smoothed_average = Sales;
		*Grab the preceding 2 and following 2 rows;
		do i = -2, -1, 1, 2; 
			GetPoint = _N_ + i;
			set have(keep=Sales Rename=(Sales=PriorSales)) point=GetPoint;
			smoothed_average + PriorSales;
		end;
		smoothed_average = smoothed_average / 5;
	end; else
		smoothed_average = .;
	drop i PriorSales;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The third line may need to change depending on your actual data to start and stop calculating properly.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 13:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417750#M280386</guid>
      <dc:creator>mpordon</dc:creator>
      <dc:date>2017-12-01T13:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate a modified version of moving averages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417756#M280387</link>
      <description>&lt;P&gt;You want a size 5 window centered on the current observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC EXPAND is the way to go, assuming you have the sas/ets product. But if you don't, then this program will do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input week sales promo;
cards;
1 792 0
2 271 0
3 490 0
4 1095 0
5 1439 0
6 597 0
7 588 0
8 7078 0
9 612 0
10 555 0
run;
data want (drop=leading_value);
  merge have  
        have (firstobs=3 keep=sales rename=(sales=leading_value));
  smoothed_average=ifn(_n_&amp;gt;=3,mean(lag2(sales),lag(sales),sales,lag(leading_value),leading_value),.);
  if leading_value=. then smoothed_average=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes there are no missing values for sales, which&amp;nbsp; means&amp;nbsp;"if leading_value=. then smoothed_average=.;" statement will prevent unwanted averages at the end of the data set.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2017 14:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-a-modified-version-of-moving-averages/m-p/417756#M280387</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-01T14:21:20Z</dc:date>
    </item>
  </channel>
</rss>

