<?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 Moving average with proc expand excluding the current observation in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734857#M80403</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am trying to use proc expand to calculate moving average of for example 3 last observations excluding the current observation. Suppose the data is like this:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Price&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;Using this code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=have out=want method=none;
convert price=price_avg / TRANSFORMOUT=(movave 3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Price&amp;nbsp; &amp;nbsp; Price_avg&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;
&lt;P&gt;Now, consider observation 4, I want the moving average does not consider the current observation. So the Price-avg should be (20+0+10)/3=10. Is there any way to do that directly in the proc expand?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Sat, 17 Apr 2021 05:05:58 GMT</pubDate>
    <dc:creator>Bright</dc:creator>
    <dc:date>2021-04-17T05:05:58Z</dc:date>
    <item>
      <title>Moving average with proc expand excluding the current observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734857#M80403</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am trying to use proc expand to calculate moving average of for example 3 last observations excluding the current observation. Suppose the data is like this:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Price&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;/P&gt;
&lt;P&gt;Using this code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=have out=want method=none;
convert price=price_avg / TRANSFORMOUT=(movave 3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Price&amp;nbsp; &amp;nbsp; Price_avg&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;
&lt;P&gt;Now, consider observation 4, I want the moving average does not consider the current observation. So the Price-avg should be (20+0+10)/3=10. Is there any way to do that directly in the proc expand?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 05:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734857#M80403</guid>
      <dc:creator>Bright</dc:creator>
      <dc:date>2021-04-17T05:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average with proc expand excluding the current observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734862#M80404</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;You should specify a window with&amp;nbsp;&lt;SPAN style="font-family: inherit;"&gt;weights in parentheses for the MOVAVE transformation operation. And you give a weight of 0 to the current observation.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Price;
cards;
1         10
2          0
3         20
4         50
;
run;
proc expand data=have out=want method=none;
convert price=price_avg / TRANSFORMOUT=(movave (0.33333 0.33333 0.33333 0));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;(If you don't like this approach, you will have to use PROC TIMEDATA or the datastep.)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Cheers,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Koen&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Apr 2021 07:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734862#M80404</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-04-17T07:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: Moving average with proc expand excluding the current observation</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734867#M80405</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Price;
cards;
1         10
2          0
3         20
4         50
;

data want;
 set have;
 array x{0:2} _temporary_;
 want=mean(of x{*});
 x{mod(_n_,3)}=price;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Apr 2021 10:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Moving-average-with-proc-expand-excluding-the-current/m-p/734867#M80405</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-04-17T10:38:09Z</dc:date>
    </item>
  </channel>
</rss>

