<?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: Help with moving average to replicate Excel approach in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185054#M303673</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This could written in a slightly simpler manner:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro calculation(nbrec=);&lt;/P&gt;&lt;P&gt;data WANT; &lt;/P&gt;&lt;P&gt;set HAVE;&lt;/P&gt;&lt;P&gt;drop LAG_GAIN1-LAG_GAIN&amp;amp;nbrec;&lt;/P&gt;&lt;P&gt;retain LAG_AVRG_GAIN LAG_AVRG_LOSS;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LAG_GAIN&amp;amp;i.=lag&amp;amp;i.(GAIN);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LAG_LOSS&amp;amp;i.=lag&amp;amp;i.(LOSS);&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;if _N_=&amp;amp;nbrec. then do;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;AVRG_GAIN=sum(of LAG_GAIN1-LAG_GAIN%eval(&amp;amp;nbrec.-1),GAIN)/&amp;amp;nbrec.;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;AVRG_LOSS=sum(of LAG_LOSS1-LAG_LOSS%eval(&amp;amp;nbrec.-1),LOSS)/&amp;amp;nbrec.;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if _N_&amp;gt;&amp;amp;nbrec. then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AVRG_GAIN=(LAG_AVRG_GAIN*&amp;amp;nbrec.+GAIN)/&amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AVRG_LOSS=(LAG_AVRG_LOSS*&amp;amp;nbrec.+LOSS)/&amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;LAG_AVRG_GAIN=AVRG_GAIN;&lt;/P&gt;&lt;P&gt;LAG_AVRG_LOSS=AVRG_LOSS;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I doubt this is correct though. You should probably divide by&amp;nbsp; (&amp;amp;nbrec.+1)&amp;nbsp; if _N_&amp;gt;&amp;amp;nbrec.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 27 Feb 2015 09:09:20 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2015-02-27T09:09:20Z</dc:date>
    <item>
      <title>Help with moving average to replicate Excel approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185052#M303671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-size: 10pt; font-family: arial, helvetica, sans-serif;"&gt;Hi Everyone,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-size: 10pt; font-family: arial, helvetica, sans-serif;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-size: 10pt; font-family: arial, helvetica, sans-serif;"&gt;This code below include 2 moving average rules. &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-size: 10pt; font-family: arial, helvetica, sans-serif;"&gt;The first rule is applied when _n_=3: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;avrg_gain=sum(l1,l2,GAIN)/&lt;/SPAN&gt;&lt;SPAN style="color: teal; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: #000080; background: white; font-size: 13.3333330154419px; background-color: #ffffff; font-family: arial, helvetica, sans-serif;"&gt;The second rule is applied when _n_&amp;gt;3: &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;avrg_gain=(lag_avrg_gain*&lt;/SPAN&gt;&lt;SPAN style="color: teal; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;+gain)/&lt;/SPAN&gt;&lt;SPAN style="color: teal; background: white; font-size: 10pt; font-family: 'Courier New';"&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;This code works but I want to put in Macro so I can change number of record to get average, say 4, 5...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please help me with that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HHC&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input gain loss;&lt;/P&gt;&lt;P&gt;&amp;nbsp; datalines;&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;2 3&lt;/P&gt;&lt;P&gt;3 4&lt;/P&gt;&lt;P&gt;4 2&lt;/P&gt;&lt;P&gt;5 1&lt;/P&gt;&lt;P&gt;6 9&lt;/P&gt;&lt;P&gt;7 0&lt;/P&gt;&lt;P&gt;8 1&lt;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;data want; set have;&lt;/P&gt;&lt;P&gt;drop l1-l2 ln1-ln2;&lt;/P&gt;&lt;P&gt;l1=lag1(gain);l2=lag2(gain);&lt;/P&gt;&lt;P&gt;ln1=lag1(loss);ln2=lag2(loss);&lt;/P&gt;&lt;P&gt;retain lag_avrg_gain lag_avrg_loss;&lt;/P&gt;&lt;P&gt;if _N_=3 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_gain=sum(l1,l2,GAIN)/3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_loss=sum(ln1,ln2,LOSS)/3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;if _N_&amp;gt;3 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_gain=(lag_avrg_gain*3+gain)/3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_loss=(lag_avrg_loss*3+loss)/3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;lag_avrg_gain=avrg_gain;&lt;/P&gt;&lt;P&gt;lag_avrg_loss=avrg_loss;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Feb 2015 00:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185052#M303671</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2015-02-27T00:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with moving average to replicate Excel approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185053#M303672</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can do the lag variables doing&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro calculation(&lt;SPAN style="font-size: 13.3333330154419px;"&gt;num_records=&lt;/SPAN&gt;);&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-style: inherit; font-size: 10pt; color: navy; background-position: initial;"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt; want; &lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;set&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt; have;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;drop&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt; l1-l20 ln1-ln20;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: blue; background: white;"&gt;retain&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt; lag_avrg_gain lag_avrg_lo&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%do i=1 %to &amp;amp;num_records;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; l&amp;amp;i.=lag&amp;amp;i.(grain);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;IF &lt;/SPAN&gt;&lt;SPAN&gt;_N_=&amp;amp;num_records &lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em; font-weight: inherit; font-style: inherit; color: blue; background-position: initial;"&gt;then do;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em; font-weight: inherit; font-style: inherit; color: black; background-position: initial;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_gain=sum(%do i=1 %to %eval(&amp;amp;num_records-1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; l&amp;amp;i.,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN style="font-size: 10pt; color: black; background: white;"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;GAIN)/&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;avrg_loss&lt;/SPAN&gt;=sum(%do i=1 %to %eval(&amp;amp;num_records-1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ln&amp;amp;i.,&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-size: 10pt; color: black; background: white;"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;LOSS)/&lt;SPAN style="font-size: 13.3333330154419px;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-size: 10pt; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13.3333330154419px;"&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt; _N_&amp;gt;&lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;then&lt;/SPAN&gt; &lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;do&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_gain=(lag_avrg_gain*&lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;+gain)/&lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avrg_loss=(lag_avrg_loss*&lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;+loss)/&lt;SPAN style="color: #000000; font-size: 13.3333330154419px; background-color: #ffffff;"&gt;&amp;amp;num_records&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: blue; background-position: initial;"&gt;end&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; font-family: 'Courier New'; color: black; background-position: initial;"&gt;lag_avrg_gain=avrg_gain;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; font-family: 'Courier New'; color: black; background-position: initial;"&gt;lag_avrg_loss=avrg_loss;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;/P&gt;&lt;P style="font-size: 13px; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: #ffffff;"&gt;&lt;SPAN&gt;&lt;SPAN style="font-style: inherit; font-size: 10pt; color: navy; background-position: initial;"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 10pt; color: black; background-position: initial;"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&lt;SPAN&gt;&lt;STRONG style="color: #000080; font-size: 13.3333330154419px; background-color: #ffffff; font-family: 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;"&gt;&lt;STRONG&gt;%mend;&lt;/STRONG&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; font-family: 'Courier New'; color: black; background: white;"&gt;Sorry for the color, I don't know how put the sames that SAS&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Feb 2015 07:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185053#M303672</guid>
      <dc:creator>arodriguez</dc:creator>
      <dc:date>2015-02-27T07:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Help with moving average to replicate Excel approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185054#M303673</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This could written in a slightly simpler manner:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro calculation(nbrec=);&lt;/P&gt;&lt;P&gt;data WANT; &lt;/P&gt;&lt;P&gt;set HAVE;&lt;/P&gt;&lt;P&gt;drop LAG_GAIN1-LAG_GAIN&amp;amp;nbrec;&lt;/P&gt;&lt;P&gt;retain LAG_AVRG_GAIN LAG_AVRG_LOSS;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LAG_GAIN&amp;amp;i.=lag&amp;amp;i.(GAIN);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LAG_LOSS&amp;amp;i.=lag&amp;amp;i.(LOSS);&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;if _N_=&amp;amp;nbrec. then do;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;AVRG_GAIN=sum(of LAG_GAIN1-LAG_GAIN%eval(&amp;amp;nbrec.-1),GAIN)/&amp;amp;nbrec.;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt; &lt;/TD&gt;&lt;TD&gt;AVRG_LOSS=sum(of LAG_LOSS1-LAG_LOSS%eval(&amp;amp;nbrec.-1),LOSS)/&amp;amp;nbrec.;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if _N_&amp;gt;&amp;amp;nbrec. then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AVRG_GAIN=(LAG_AVRG_GAIN*&amp;amp;nbrec.+GAIN)/&amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AVRG_LOSS=(LAG_AVRG_LOSS*&amp;amp;nbrec.+LOSS)/&amp;amp;nbrec.;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;LAG_AVRG_GAIN=AVRG_GAIN;&lt;/P&gt;&lt;P&gt;LAG_AVRG_LOSS=AVRG_LOSS;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I doubt this is correct though. You should probably divide by&amp;nbsp; (&amp;amp;nbrec.+1)&amp;nbsp; if _N_&amp;gt;&amp;amp;nbrec.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Feb 2015 09:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185054#M303673</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2015-02-27T09:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help with moving average to replicate Excel approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185055#M303674</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry, its not very clear for me.&amp;nbsp; I have looked at your code, to which there are several mistakes, e.g. missing semi-colon after retain statement, plus many non-initialised variables.&amp;nbsp; I am not clear on the logic either, why for instance is a calculation only done at observation number 3, what happens if the data is not sorted as you expect it to be, or there is only 2 observations?&amp;nbsp; If you really are only dealing with 8 instances, then you may be better off working with arrays and a macro variable (i.e. data is horizontal rather than vertical):&lt;/P&gt;&lt;P&gt;%let Num_Iterations=4;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array ln{*};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array l{*};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; avg=sum(of l1-l3);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please show what you want out.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Feb 2015 09:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185055#M303674</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-02-27T09:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Help with moving average to replicate Excel approach</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185056#M303675</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I correct that error. &lt;/P&gt;&lt;P&gt;I have to sort data prior to processing.&lt;/P&gt;&lt;P&gt;The calculate start at 3 as a rule I have to follow. For the macro, I can do it with 4, 5....&lt;/P&gt;&lt;P&gt;HHC&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 27 Feb 2015 14:50:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-moving-average-to-replicate-Excel-approach/m-p/185056#M303675</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2015-02-27T14:50:04Z</dc:date>
    </item>
  </channel>
</rss>

