<?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: How to create new rows with moving average calculation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592033#M169707</link>
    <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for the confusion. Still new in posting questions here. Your answer is kind of what I need, but I also need to use the forecasted moving average data for the future month forecast, let me show your an example: for proportion data, I only have actuals till 1/8/2019, then I want to have the three month moving average for Sep'19 to Dec'19, using forecasted data as an input too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;date           proportion                moving_average
1/4/2019         0.1
1/5/2019         0.2
1/6/2019         0.3                     
1/7/2019         0.25                    0.2 (average of 0.1, 0.2, 0.3)
1/8/2019         0.5                     0.75 （average of 0.2, 0.3, 0.25)&lt;BR /&gt;1/9/2019                                 0.35 (average of 0.3, 0.25, 0.5)&lt;BR /&gt;1/10/2019                                0.37 (average of 0.25, 0.5, 0.35(forecasted)) &lt;BR /&gt;1/11/2019                                0.41 (average of 0.5, 0.35(forecasted), 0.37(forecasted))&lt;BR /&gt;1/12/2019                                0.38 (average of 0.35, 0.37, 0.41)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I tried proc expand, which gives exactly the result you offered. I also tried proc arima to compute the moving average. The interesting part by using proc arima is that, for the computed moving average, there will be a slightly difference comparing the result that I manually computed in excel (in 10e-4 level). Since this ratio is needed to multiply a big number (10 billion level), a slight difference will produce a big number after that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering if any other simple way we can do the moving average with the forecasted data?&lt;/P&gt;</description>
    <pubDate>Thu, 26 Sep 2019 21:19:47 GMT</pubDate>
    <dc:creator>shasha11111</dc:creator>
    <dc:date>2019-09-26T21:19:47Z</dc:date>
    <item>
      <title>How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591711#M169564</link>
      <description>&lt;P&gt;Hi Guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need your help to suggest the code to create the 2 month moving average for proportion and value from 01/04/2019 to 01/08/2019.&lt;/P&gt;&lt;P&gt;Tried proc expand but it only computes the moving average based on the observed data (01-03, 2019, in this case) not the forecast data. Any simple clean code can be used to produce the values in the blank cells?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Proportion&amp;nbsp; &amp;nbsp; &amp;nbsp; Value&lt;/P&gt;&lt;P&gt;01/01/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;01/02/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;&lt;P&gt;01/03/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;/P&gt;&lt;P&gt;01/04/2019&lt;/P&gt;&lt;P&gt;01/05/2019&lt;/P&gt;&lt;P&gt;01/06/2019&lt;/P&gt;&lt;P&gt;01/07/2019&lt;/P&gt;&lt;P&gt;01/08/2019&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 00:00:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591711#M169564</guid>
      <dc:creator>shasha11111</dc:creator>
      <dc:date>2019-09-26T00:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591713#M169566</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292340"&gt;@shasha11111&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Guys,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need your help to suggest the code to create the observations till 01Dec2024, with the 3 moving average is calculated for the dataset as below:&lt;/P&gt;
&lt;P&gt;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Proportion&lt;/P&gt;
&lt;P&gt;01/01/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.1&lt;/P&gt;
&lt;P&gt;01/02/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.2&lt;/P&gt;
&lt;P&gt;01/03/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.3&lt;/P&gt;
&lt;P&gt;?&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3m moving average&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's not clear to me what goes on row 4 in column 2 where you have written 3m moving average. What goes in row 5 column 2? Please explain clearly what calculation goes there.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2019 21:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591713#M169566</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-25T21:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591731#M169570</link>
      <description>I want to compute the Jan Feb Mar proportion as for row 4 01/04/2019. So as to the 5 and 6 th rows til 01/12/2022. The proportion is the value of three month moving average.&lt;BR /&gt;&lt;BR /&gt;I tried to use proc expand, but it only produces the moving average based on the observed data not the forecasted data.</description>
      <pubDate>Wed, 25 Sep 2019 23:30:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591731#M169570</guid>
      <dc:creator>shasha11111</dc:creator>
      <dc:date>2019-09-25T23:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591808#M169605</link>
      <description>&lt;P&gt;But there is no data after the third row.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 15:18:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591808#M169605</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-26T15:18:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591928#M169664</link>
      <description>&lt;P&gt;The data after the third row is what I want to compute using the moving average. Have not figured out how to do it in SAS. Tried both proc expand a proc arima, does not work.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 17:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591928#M169664</guid>
      <dc:creator>shasha11111</dc:creator>
      <dc:date>2019-09-26T17:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591974#M169685</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292340"&gt;@shasha11111&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The data after the third row is what I want to compute using the moving average.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You want to compute data after the third row, using the moving average?? I still don't grasp the concept.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;date           proportion                moving_average
1/1/2019         0.1
1/2/2019         0.2
1/3/2019         0.3                     0.2
1/4/2019         0.25                    0.25
1/5/2019         0.15                    0.2333           &lt;/PRE&gt;
&lt;P&gt;where there is actual data under proportion at each date, and the moving average is in a separate column? The way you presented the data originally, and your words quoted above, indicate the moving average is in the same column as proportion, and no new data in column 2, only computed data; which doesn't make any sense to me.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 19:52:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/591974#M169685</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-26T19:52:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592033#M169707</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sorry for the confusion. Still new in posting questions here. Your answer is kind of what I need, but I also need to use the forecasted moving average data for the future month forecast, let me show your an example: for proportion data, I only have actuals till 1/8/2019, then I want to have the three month moving average for Sep'19 to Dec'19, using forecasted data as an input too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;date           proportion                moving_average
1/4/2019         0.1
1/5/2019         0.2
1/6/2019         0.3                     
1/7/2019         0.25                    0.2 (average of 0.1, 0.2, 0.3)
1/8/2019         0.5                     0.75 （average of 0.2, 0.3, 0.25)&lt;BR /&gt;1/9/2019                                 0.35 (average of 0.3, 0.25, 0.5)&lt;BR /&gt;1/10/2019                                0.37 (average of 0.25, 0.5, 0.35(forecasted)) &lt;BR /&gt;1/11/2019                                0.41 (average of 0.5, 0.35(forecasted), 0.37(forecasted))&lt;BR /&gt;1/12/2019                                0.38 (average of 0.35, 0.37, 0.41)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I tried proc expand, which gives exactly the result you offered. I also tried proc arima to compute the moving average. The interesting part by using proc arima is that, for the computed moving average, there will be a slightly difference comparing the result that I manually computed in excel (in 10e-4 level). Since this ratio is needed to multiply a big number (10 billion level), a slight difference will produce a big number after that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering if any other simple way we can do the moving average with the forecasted data?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 21:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592033#M169707</guid>
      <dc:creator>shasha11111</dc:creator>
      <dc:date>2019-09-26T21:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592122#M169739</link>
      <description>&lt;P&gt;So you only have 5 data points, and then after that you use the moving average as a data point? It doesn't make any sense to do this.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 10:47:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592122#M169739</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-27T10:47:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to create new rows with moving average calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592449#M169873</link>
      <description>&lt;P&gt;You don't need proc expand for something like this.&amp;nbsp; Instead just provide one record per month, including records for the extended months, but with proportion for the last 3 months taking a missing value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input date mmddyy10.    proportion ;
  format date date9. ;
datalines;
1/4/2019         0.1
1/5/2019         0.2
1/6/2019         0.3 
1/7/2019         0.25
1/8/2019         0.5
1/9/2019 
1/10/2019
1/11/2019
1/12/2019
run;
data want;
  set have;
  movavg=mean(coalesce(lag1(proportion),lag1(movavg))
             ,coalesce(lag2(proportion),lag2(movavg))
             ,coalesce(lag3(proportion),lag3(movavg))
             );
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The movavg variables is the mean of 3 values, from the 3 preceding months.&amp;nbsp;&amp;nbsp; If you never had a missing value for proportion, you could do with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  movavg=mean(lag1(proportion),lag2(proportion),lag3(proportion));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the trailng months have no available lagged value for proportion.&amp;nbsp; In those cases, you have told us you want the lagged value of the calculated moving average.&amp;nbsp; That's why I used COALESCE(lagx(proportion),lagx(movavg)), which takes the Xth lag of proportion, except if it is missing, in which case it substitues the Xth lag of movavg.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Sep 2019 00:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-new-rows-with-moving-average-calculation/m-p/592449#M169873</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-29T00:25:34Z</dc:date>
    </item>
  </channel>
</rss>

