<?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: PROC EXPAND moving average from previous 7 calendar days in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716566#M80121</link>
    <description>&lt;P&gt;Thanks Reeza! This solution works pretty well, though it does add in observations for the moving average for dates where the original data doesn't exist. I just cut out those data with a where statement in the out dataset to keep it to the original observed dates.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Feb 2021 18:01:12 GMT</pubDate>
    <dc:creator>ebowen</dc:creator>
    <dc:date>2021-02-03T18:01:12Z</dc:date>
    <item>
      <title>PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716546#M80115</link>
      <description>&lt;P&gt;I'm trying to do a moving average with PROC EXPAND where the average takes into account only those observations that occur within the previous seven calendar days, which is not necessarily the previous seven observations. I have a dataset that has some gaps, such as weekends, holidays, etc., where the data is not reported. But I don't want to average observations that are outside the weekly window. In other words, if I'm calculating the moving average on a Monday, I want the average to be only those observations from the previous Tuesday to the current day, regardless of whether there are observations for the weekend.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently PROC EXPAND is taking seven previous observations, which can average in numbers from a few days before that. Here's my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand DATA=gas.gasprices OUT=gas.GasPrices_ma METHOD=none;
ID Date;
convert spread=Spread_MA / TRANSFORMOUT = (nomiss movave 7 trim 6);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 18:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716546#M80115</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2021-02-03T18:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716554#M80117</link>
      <description>&lt;P&gt;Use PROC TIMESERIES to first fill in the gaps with missing and then use PROC EXPAND to do the moving averages.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also use a data step if so inclined, but this method is easier as it will scale to month/years/weeks as necessary.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a &lt;A href="https://gist.github.com/statgeek/07a3708dee1225ceb9d4aa75daab2c52" target="_self"&gt;fully worked example&lt;/A&gt; that shows how this is done at the monthly level, you will need to change the INTERVAL to day for your case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;/*this is an example of time series data. 
1. Create a time series data set with missing intervals (IBM)
2. Add back missing entries using PROC TIMESERIES (IBM_NO_MISSING)
3. Calculate moving average - 12 month average
*/
&lt;/STRONG&gt;
&lt;STRONG&gt;/*1*/&lt;/STRONG&gt;&lt;/FONT&gt;
data ibm;
	set sashelp.stocks;
	where stock='IBM';

	if month(date)=7 then
		delete;
run;

proc sort data=ibm;
	by date;
run;

&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;/*2*/&lt;/STRONG&gt;&lt;/FONT&gt;
proc timeseries data=ibm out=ibm_no_missing;
	id date interval=month start='01Aug1986'd end='01Dec2005'd;
	var open;
run;

&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;/*3*/&lt;/STRONG&gt;&lt;/FONT&gt;
proc expand data=ibm_no_missing out=want;
	id date;
	convert open = open_12 / method=none transformout= (movave 12);
run;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37602"&gt;@ebowen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm trying to do a moving average with PROC EXPAND where the average takes into account only those observations that occur within the previous seven calendar days, which is not necessarily the previous seven observations. I have a dataset that has some gaps, such as weekends, holidays, etc., where the data is not reported. But I don't want to average observations that are outside the weekly window. In other words, if I'm calculating the moving average on a Monday, I want the average to be only those observations from the previous Tuesday to the current day, regardless of whether there are observations for the weekend.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Currently PROC EXPAND is taking seven previous observations, which can average in numbers from a few days before that. Here's my code, data attached:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand DATA=gas.gasprices OUT=gas.GasPrices_ma METHOD=none;
ID Date;
convert spread=Spread_MA / TRANSFORMOUT = (nomiss movave 7 trim 6);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 17:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716554#M80117</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-03T17:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716566#M80121</link>
      <description>&lt;P&gt;Thanks Reeza! This solution works pretty well, though it does add in observations for the moving average for dates where the original data doesn't exist. I just cut out those data with a where statement in the out dataset to keep it to the original observed dates.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2021 18:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/716566#M80121</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2021-02-03T18:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738795#M80504</link>
      <description>Hello All&lt;BR /&gt;Can anyone tell me how to do 7 hourly moving average timeseries data?&lt;BR /&gt;&lt;BR /&gt;Using proc expand..&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 04 May 2021 07:46:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738795#M80504</guid>
      <dc:creator>akshay_solanki</dc:creator>
      <dc:date>2021-05-04T07:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738912#M80505</link>
      <description>Is your data already hourly?&lt;BR /&gt;&lt;BR /&gt;If so something as simple as this may work:&lt;BR /&gt;&lt;BR /&gt;proc expand data=have out=want method=none;&lt;BR /&gt;id datetimevariable;&lt;BR /&gt;convert  value = value7 / transout = (moveave 7);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/etsug/68148/HTML/default/viewer.htm#etsug_expand_examples04.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/etsug/68148/HTML/default/viewer.htm#etsug_expand_examples04.htm&lt;/A&gt;</description>
      <pubDate>Tue, 04 May 2021 14:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738912#M80505</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-04T14:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738916#M80506</link>
      <description>I've a same datetime 30 observation with sequence 1 and datetime format date variable and sequence variable which is group by sequence. I want to do 7 hourly moving average timeseries data and plot by sequence.&lt;BR /&gt;&lt;BR /&gt;And I've missing data also in value.</description>
      <pubDate>Tue, 04 May 2021 14:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738916#M80506</guid>
      <dc:creator>akshay_solanki</dc:creator>
      <dc:date>2021-05-04T14:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738928#M80508</link>
      <description>Please start a new thread with your question and include sample data that reflects your actual data, fake data is fine.</description>
      <pubDate>Tue, 04 May 2021 15:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/738928#M80508</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-04T15:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: PROC EXPAND moving average from previous 7 calendar days</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/739135#M80520</link>
      <description>I just add to=hour and sorting group by variable then it's work very well.&lt;BR /&gt;&lt;BR /&gt;Thank you for your help and support.&lt;BR /&gt;&lt;BR /&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_halo:"&gt;😇&lt;/span&gt;🤩</description>
      <pubDate>Wed, 05 May 2021 04:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-EXPAND-moving-average-from-previous-7-calendar-days/m-p/739135#M80520</guid>
      <dc:creator>akshay_solanki</dc:creator>
      <dc:date>2021-05-05T04:36:28Z</dc:date>
    </item>
  </channel>
</rss>

