<?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: LAG for missing values - moving average in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257755#M49533</link>
    <description>&lt;P&gt;Yes. LAG() is not dynamic. Use ARRAY instead .&lt;/P&gt;
&lt;P&gt;Post some real data to explain your question well.&lt;/P&gt;</description>
    <pubDate>Sat, 19 Mar 2016 10:50:30 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-03-19T10:50:30Z</dc:date>
    <item>
      <title>LAG for missing values - moving average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257708#M49508</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;I am trying to create moving averages by looking at lag - lag 1 and lag2 values of rewards redemption. Here is the formula &lt;SPAN&gt;current&amp;nbsp;&lt;/SPAN&gt;= (lag - lag2) / (lag2 - lag3).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, the current value is not stored sequencially in the rows on update. Meaning,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;when i find row4 current value is missing, i am using this formula to come up with row4 rewards. Now, row4 -&amp;gt; current = 100&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When moving to row5 - new rewards calculated in row4 isnt available . lag1 -&amp;gt; row4&amp;gt; current&amp;nbsp;= 0 (i guess lag values are not dynamic)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA vtest;&lt;BR /&gt;SET v10;&lt;BR /&gt;by v_id group id;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;current &amp;nbsp;&lt;/SPAN&gt;= red1;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; lag_red1 = lag(newredemption1);&lt;BR /&gt; lag2_red1 = lag2(newredemption);&lt;BR /&gt; lag3_red1 = lag3(newredemption);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; expected_increase_pts = (lag_red1*(lag_red1 / lag2_red1)) - lag2_red1;&lt;BR /&gt; previos_redeem_growth = (lag_red1 - lag2_red1) / (lag2_red1 - lag3_red1);&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;current &amp;nbsp;&lt;/SPAN&gt;= &lt;SPAN&gt;current &amp;nbsp;&lt;/SPAN&gt;+ (expected_increase_pts * previos_redeem_growth);&lt;/P&gt;
&lt;P&gt;retain &lt;SPAN&gt;current&amp;nbsp;&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 21:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257708#M49508</guid>
      <dc:creator>arunmmw</dc:creator>
      <dc:date>2016-03-18T21:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: LAG for missing values - moving average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257716#M49513</link>
      <description>&lt;P&gt;It would really help to understand what you are attempting if you provide some data in the form of a datastep for input and what you expect for the output for that example data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do wonder, since you say "current" is sometimes missing that you want&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;current &amp;nbsp;&lt;/SPAN&gt;= sum(&lt;SPAN&gt;current&amp;nbsp;,&lt;/SPAN&gt; (expected_increase_pts * previos_redeem_growth));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something to consider: you need to address all of the potential missing values before doing division with the lagged values.&lt;/P&gt;
&lt;P&gt;Lag3 will not exist until you get to the 4th observation. So you are going to have division by missing which&amp;nbsp;you may want to consider.&lt;/P&gt;
&lt;P&gt;And if your newredemption&amp;nbsp;variable is evermissing you're going to get intermittent other calculations with missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lag values are only for variables read in through a SET or MERGE statement. If you want to keep a calculated variable then you use RETAIN. When to reset to initial values or missing&amp;nbsp;for retained variables is sometimes interesting. You might want something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain TempCurrent 0; /* this set an intial value of 0*/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;misc calculations&amp;gt;&lt;/P&gt;
&lt;P&gt;if missing(current) then current=sum(TempCurrent,&amp;nbsp;(expected_increase_pts * previos_redeem_growth));&lt;/P&gt;
&lt;P&gt;else current = &amp;nbsp;sum(&lt;SPAN&gt;current&amp;nbsp;,&lt;/SPAN&gt; (expected_increase_pts * previos_redeem_growth));&lt;/P&gt;
&lt;P&gt;/* and then reset the retained value*/&lt;/P&gt;
&lt;P&gt;TempCurrent = current;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Mar 2016 22:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257716#M49513</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-18T22:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: LAG for missing values - moving average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257755#M49533</link>
      <description>&lt;P&gt;Yes. LAG() is not dynamic. Use ARRAY instead .&lt;/P&gt;
&lt;P&gt;Post some real data to explain your question well.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2016 10:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/257755#M49533</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-03-19T10:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: LAG for missing values - moving average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/258793#M49921</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;SPAN class="login-bold"&gt;&lt;A id="link_13" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884" target="_self"&gt;ballardw&lt;/A&gt;&amp;nbsp;and ksharp. Appreciate you both helping out&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2016 13:32:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-for-missing-values-moving-average/m-p/258793#M49921</guid>
      <dc:creator>arunmmw</dc:creator>
      <dc:date>2016-03-24T13:32:28Z</dc:date>
    </item>
  </channel>
</rss>

