<?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: Need to grab a lag value before a calculation, then store the next lag value after the calculati in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286520#M58787</link>
    <description>&lt;P&gt;Numbers are worth thousand words.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a very simple array approach. No need to use lag() function. &amp;nbsp;I assumed your months are numbers like 1 , 2 , ...&lt;/P&gt;&lt;P&gt;But it doesn't affect the array approach. The steps are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[1] Store the values of A into an array (K[]) which is indexed by MONTH.&lt;/P&gt;&lt;P&gt;[2] Getting the lag1 for Month i is simply to get the value of A corresponding to (i-1)th month.&lt;/P&gt;&lt;P&gt;[3] When A is missing, we use the lag value, compute Z and replace the missing value by Z in the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array k[9] _temporary_;
   do until(eof);
      set have end = eof;
      k[month] = a;
   end;
   month = 1;
   a = k[1];
   output;
   do i = 2 to 9;
      month = i;
      if k[i] = . then do;
         lag_a = k[i-1];
         z = 2 * lag_a;
         a = z;
         k[i] = a;
      end;
      else do;
         a = k[i];
         lag_a = k[i - 1];
         z = 2 * lag_a;
      end;
      output;
   end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you have any problem to handle the array because of then values of month, come back and tell how your real months are. It can be handled by the array.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this solution is acceptable to you.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jul 2016 23:52:51 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2016-07-22T23:52:51Z</dc:date>
    <item>
      <title>Need to grab a lag value before a calculation, then store the next lag value after the calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286421#M58729</link>
      <description>&lt;P&gt;I'm using PC SAS 9.4M3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've done some time series modeling, generated scoring code, and I'm trying to predict into the future.&amp;nbsp; But the issue I'm having is that I've been aksed to use AdaptiveReg and not TimeSeries so I had to code the lag variables myself. No problem there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where I'm running into issues is with the circular logic, for each row in&amp;nbsp;my data, after the actuals, I need to grab the previous value, score the model, then store that prediction so I can grab it for the next row.&amp;nbsp; Sounds simple enough.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for a simplificaion let's say I have a dataset like this where A is my dep var and Z is my prediction.&amp;nbsp; It is simply 2*LAG_A. I have 4 months of data and I'm trying to predict out to month 9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="256" style="width: 192pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" span="4" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" style="border: 0px black; border-image: none; width: 48pt; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;month&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;a&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;lag_a&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;z&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;.&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;.&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;10&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;7&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;8&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;9&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;I tried something like the following:&lt;/P&gt;
&lt;P&gt;data scored;&lt;/P&gt;
&lt;P&gt;set actuals;/*the above table without column z*/&lt;/P&gt;
&lt;P&gt;z=2*Lag_A;&lt;/P&gt;
&lt;P&gt;if a=. then a=z;/*take the predicted value as my dep var where I have no actuals*/&lt;/P&gt;
&lt;P&gt;temp_lag=lag(a);/*I want the lag to execute each time so all values go into the queue*/&lt;/P&gt;
&lt;P&gt;if lag_a=. then lag_a=temp_lag;/*only fill in if I don't have the actual value already there*/&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this works great for month 4, but not for month 5 since I need the lag statement before I calculate z (otherwise I don't have a value in lag_a), BUT I can't really do that since it loads the value of a to the queue and before I calculate Z, A is missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to do the lag before and after, thinking that doing it before would grab the value from the queue while loading a missing to the queue, and then doing it again after scoring would put the correct value in the queue, but that didn't work as I expected either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data scored;&lt;/P&gt;
&lt;P&gt;set actuals;/*the above table without column z*/&lt;/P&gt;
&lt;P&gt;temp_lag=lag(a);/*grab last value from queue*/&lt;/P&gt;
&lt;P&gt;z=2*Lag_A;&lt;/P&gt;
&lt;P&gt;if a=. then a=z;/*take the predicted value as my dep var where I have no actuals*/&lt;/P&gt;
&lt;P&gt;temp_lag=lag(a);/*write the new value of a to the queue*/&lt;/P&gt;
&lt;P&gt;if lag_a=. then lag_a=temp_lag;/*only fill in if I don't have the actual value already there*/&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only change is that lag_a does get filled in, but z still doesn't calculate correctly, it's still getting missing values from the queue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I thought maybe I coudl do SYMGET before calculating Z and then do a CALL SYMPUT after calculations, but my understanding is that the macro var CALL SYMPUT creates isn't available till after the RUN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any hope?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2016 15:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286421#M58729</guid>
      <dc:creator>ANWZimmerman</dc:creator>
      <dc:date>2016-07-22T15:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: Need to grab a lag value before a calculation, then store the next lag value after the calculati</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286427#M58733</link>
      <description>&lt;P&gt;Store for the NEXT row would use RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think what you want is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retain Lag_a;&lt;/P&gt;
&lt;P&gt;BUT you need to assign a value to it for everytime you expect to need it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't tell if the data you show is what you are getting or want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro varibles are really not needed and very likelyt to not work the way you want.&lt;/P&gt;
&lt;P&gt;You didn't give much of an example but this may be what your are looking for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scored;
   set actuals;/*the above table without column z*/
   retain lag_A .;
   temp_lag=lag(a);/*write the new value of a to the queue*/

   if lag_a=. then lag_a=temp_lag;/*only fill in if I don't have the actual value already there*/
   z=2*Lag_a;
   if a=. then a=z;
   lag_A =a;  /* saves the current value of a */

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jul 2016 16:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286427#M58733</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-22T16:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Need to grab a lag value before a calculation, then store the next lag value after the calculati</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286442#M58738</link>
      <description>The problem with retain is that I don't want the new record's lag_a to equal the previous record's lag_a, I need it to equal the last records A.&lt;BR /&gt;&lt;BR /&gt;After chatting with my boss, I'm going to explore HASH.  I think it will give me the flexibility to grab the value of A from the previous record, store that as Lag_a for this record, calculate Z, then store that as A, then be able to grab that value at the start of the next row's calculations.</description>
      <pubDate>Fri, 22 Jul 2016 17:27:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286442#M58738</guid>
      <dc:creator>ANWZimmerman</dc:creator>
      <dc:date>2016-07-22T17:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: Need to grab a lag value before a calculation, then store the next lag value after the calculati</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286443#M58739</link>
      <description>&lt;P&gt;I see now that I lost the desired results. (I had typed it all up, had to take care of something else, came back, made sure it was ready to post, hit post, but I had timed out so I lost the whole post)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is what I want to end up with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="256" style="width: 192pt; border-collapse: collapse;" border="0" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL width="64" style="width: 48pt;" span="4" /&gt;&lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD width="64" height="20" style="border: 0px black; border-image: none; width: 48pt; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;month&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;a&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;lag_a&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="64" style="border: 0px black; border-image: none; width: 48pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;z&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;1&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;.&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;.&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;2&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;3&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;4&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;10&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;10&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;5&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;20&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;10&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;20&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;6&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;40&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;20&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;40&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;7&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;80&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;40&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;80&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;8&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;160&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;80&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;160&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15pt;"&gt;
&lt;TD height="20" style="border: 0px black; border-image: none; height: 15pt; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;9&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;320&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;160&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD style="border: 0px black; border-image: none; background-color: transparent;"&gt;&lt;FONT color="#000000" face="Calibri" size="3"&gt;320&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2016 17:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286443#M58739</guid>
      <dc:creator>ANWZimmerman</dc:creator>
      <dc:date>2016-07-22T17:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: Need to grab a lag value before a calculation, then store the next lag value after the calculati</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286472#M58754</link>
      <description>&lt;P&gt;Before leaving a record the code I provided sets the Lag_a value to the value of A, not to Lag(a).&lt;/P&gt;
&lt;P&gt;Did you try running the code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic for Hash is going to be very similar for Retain.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2016 20:01:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286472#M58754</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-22T20:01:26Z</dc:date>
    </item>
    <item>
      <title>Re: Need to grab a lag value before a calculation, then store the next lag value after the calculati</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286520#M58787</link>
      <description>&lt;P&gt;Numbers are worth thousand words.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a very simple array approach. No need to use lag() function. &amp;nbsp;I assumed your months are numbers like 1 , 2 , ...&lt;/P&gt;&lt;P&gt;But it doesn't affect the array approach. The steps are:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[1] Store the values of A into an array (K[]) which is indexed by MONTH.&lt;/P&gt;&lt;P&gt;[2] Getting the lag1 for Month i is simply to get the value of A corresponding to (i-1)th month.&lt;/P&gt;&lt;P&gt;[3] When A is missing, we use the lag value, compute Z and replace the missing value by Z in the array.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array k[9] _temporary_;
   do until(eof);
      set have end = eof;
      k[month] = a;
   end;
   month = 1;
   a = k[1];
   output;
   do i = 2 to 9;
      month = i;
      if k[i] = . then do;
         lag_a = k[i-1];
         z = 2 * lag_a;
         a = z;
         k[i] = a;
      end;
      else do;
         a = k[i];
         lag_a = k[i - 1];
         z = 2 * lag_a;
      end;
      output;
   end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If you have any problem to handle the array because of then values of month, come back and tell how your real months are. It can be handled by the array.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this solution is acceptable to you.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2016 23:52:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-grab-a-lag-value-before-a-calculation-then-store-the/m-p/286520#M58787</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2016-07-22T23:52:51Z</dc:date>
    </item>
  </channel>
</rss>

