<?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: Calulate Average of previous and Next value in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66097#M18849</link>
    <description>You can also find DATA step examples at:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/25/027.html" target="_blank"&gt;http://support.sas.com/kb/25/027.html&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
PROC EXPAND can also be used to calculate moving averages, see:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/forum2008/093-2008.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2008/093-2008.pdf&lt;/A&gt;</description>
    <pubDate>Sun, 22 Aug 2010 04:16:24 GMT</pubDate>
    <dc:creator>ArtC</dc:creator>
    <dc:date>2010-08-22T04:16:24Z</dc:date>
    <item>
      <title>Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66094#M18846</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I need to calculate Average as shown in below table,&lt;BR /&gt;
by zone and time.&lt;BR /&gt;
&lt;BR /&gt;
Average should be of previous and the next number &lt;BR /&gt;
for ex: for zone A for time 00:30 it is average of demand of time 00:15 and 00:45.&lt;BR /&gt;
&lt;BR /&gt;
Time	Zone	demand	Average&lt;BR /&gt;
0:15	A	10	10&lt;BR /&gt;
0:30	A	20	20&lt;BR /&gt;
0:45	A	30	17.5&lt;BR /&gt;
0:15	B	15	15&lt;BR /&gt;
0:30	B	18	17.5&lt;BR /&gt;
0:45	B	20	17.5&lt;BR /&gt;
0:15	C	17	17&lt;BR /&gt;
0:30	C	19	19&lt;BR /&gt;
0:45	C	21	11&lt;BR /&gt;
0:15	D	3	3&lt;BR /&gt;
0:30	D	8	11&lt;BR /&gt;
0:45	D	19	8&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
please help regarding the code.&lt;BR /&gt;
thanks</description>
      <pubDate>Sat, 21 Aug 2010 15:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66094#M18846</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-08-21T15:43:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66095#M18847</link>
      <description>Explore using the LAG function - the SAS DOC section below demonstrates a DATA step approach to obtaining a "Leading Series" calculation.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/etsug_tsdata_sect051.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/etsug/60372/HTML/default/etsug_tsdata_sect051.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Suggested Google advanced search argument, this topic / post:&lt;BR /&gt;
&lt;BR /&gt;
lag function site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
next observation value calculation site:sas.com&lt;BR /&gt;
&lt;BR /&gt;
lead function site:sas.com</description>
      <pubDate>Sat, 21 Aug 2010 18:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66095#M18847</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-08-21T18:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66096#M18848</link>
      <description>While I don't understand your example, the code offered by Howard Schrier at:&lt;BR /&gt;
&lt;A href="http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back" target="_blank"&gt;http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back&lt;/A&gt;&lt;BR /&gt;
will definitely give you a way to average the previous and next values.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Sat, 21 Aug 2010 22:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66096#M18848</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-08-21T22:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66097#M18849</link>
      <description>You can also find DATA step examples at:&lt;BR /&gt;
&lt;A href="http://support.sas.com/kb/25/027.html" target="_blank"&gt;http://support.sas.com/kb/25/027.html&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
PROC EXPAND can also be used to calculate moving averages, see:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/forum2008/093-2008.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2008/093-2008.pdf&lt;/A&gt;</description>
      <pubDate>Sun, 22 Aug 2010 04:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/66097#M18849</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-08-22T04:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/814476#M81815</link>
      <description>It really helped. Thank you!</description>
      <pubDate>Fri, 20 May 2022 17:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/814476#M81815</guid>
      <dc:creator>fangfangfu</dc:creator>
      <dc:date>2022-05-20T17:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calulate Average of previous and Next value</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/829078#M81980</link>
      <description>&lt;P&gt;You can use the &lt;STRONG&gt;cmovave&lt;/STRONG&gt; transformation operation in PROC EXPAND:&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;
    by zone;
    id time;
    convert demand = avg / transform=(cmovave 3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want a pure DATA step approach, you can calculate a lag and lead, then divide. Attached is a program that will let you calculate leads efficiently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%lead(data=have, out=have_lead, var=demand, by=zone);

data want;
    set have_lead;
    by zone time;

    lag1_demand = lag(demand);

    /* Calculate the denominator and do not set lags for the first value of zone */
    if(first.zone) then do;
        n = 1; 
        lag1_demand = .;
    end;
        else n = 3 - nmiss(lag1_demand, lead1_demand);

    avg = sum(demand, lag1_demand, lead1_demand)/n;

    drop lag1_demand lead1_demand n;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 17:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calulate-Average-of-previous-and-Next-value/m-p/829078#M81980</guid>
      <dc:creator>Stu_SAS</dc:creator>
      <dc:date>2022-08-17T17:01:27Z</dc:date>
    </item>
  </channel>
</rss>

