<?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: How to retain a value from the previous row to do calculations on current row? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472643#M285819</link>
    <description>Thank you. Getting closer. But not all records are being calculated.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Here is the sample code:&lt;BR /&gt;&lt;BR /&gt;/* Sample dataset */&lt;BR /&gt;data temp1;&lt;BR /&gt;input month state ts mt;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 90 .&lt;BR /&gt;2 1 95 .&lt;BR /&gt;3 1 100 .&lt;BR /&gt;4 1 110 115&lt;BR /&gt;5 1 115 120&lt;BR /&gt;6 1 120 130&lt;BR /&gt;1 2 1000 .&lt;BR /&gt;2 2 1100 .&lt;BR /&gt;3 2 950 .&lt;BR /&gt;4 2 1050 1150&lt;BR /&gt;5 2 1125 1200&lt;BR /&gt;6 2 1200 1300&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* Sort by state and descending month */&lt;BR /&gt;proc sort data=temp1 out=temp2;&lt;BR /&gt;by state descending month;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp2;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* TS growth rates */&lt;BR /&gt;data temp3;&lt;BR /&gt;set temp2;&lt;BR /&gt;by state descending month;&lt;BR /&gt;&lt;BR /&gt;g=ts/lag(ts)-1;&lt;BR /&gt;if first.state then g=.;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp3;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/* Backfill MT series */&lt;BR /&gt;data temp4;&lt;BR /&gt;set temp3;&lt;BR /&gt;by state descending month;&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;retain x;&lt;BR /&gt;if first.state then x=.;&lt;BR /&gt;if mt ne . then x=mt;&lt;BR /&gt;else if mt eq . then x=lag(x)*(1+g);&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;retain mt2;&lt;BR /&gt;if first.state then mt2=.;&lt;BR /&gt;mt2=min(lag(mt2)*(1+g_ts),mt);&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;Lmt=Lag(mt);&lt;BR /&gt;if not missing(mt) then x=Lmt*(1+g);&lt;BR /&gt;else x=Lmt*(1+g);&lt;BR /&gt;&lt;BR /&gt;if mt ne . then x=mt;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp4;&lt;BR /&gt;format x best12.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This is the output from the last print-x is null for latter records.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Obs month state ts mt g lmt x&lt;BR /&gt;&lt;BR /&gt;1 6 1 120 130 . . 130&lt;BR /&gt;2 5 1 115 120 -0.04167 130 120&lt;BR /&gt;3 4 1 110 115 -0.04348 120 115&lt;BR /&gt;4 3 1 100 . -0.09091 115 104.54545455&lt;BR /&gt;5 2 1 95 . -0.05000 . .&lt;BR /&gt;6 1 1 90 . -0.05263 . .&lt;BR /&gt;7 6 2 1200 1300 . . 1300&lt;BR /&gt;8 5 2 1125 1200 -0.06250 1300 1200&lt;BR /&gt;9 4 2 1050 1150 -0.06667 1200 1150&lt;BR /&gt;10 3 2 950 . -0.09524 1150 1040.4761905&lt;BR /&gt;11 2 2 1100 . 0.15789 . .&lt;BR /&gt;12 1 2 1000 . -0.09091 . .&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 22 Jun 2018 23:33:15 GMT</pubDate>
    <dc:creator>su17</dc:creator>
    <dc:date>2018-06-22T23:33:15Z</dc:date>
    <item>
      <title>How to retain a value from the previous row to do calculations on current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472637#M285817</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;&lt;CODE class=" language-sas"&gt;month state ts mt g

6 1 120 130 .

5 1 115 120 -0.04167

4 1 110 115 -0.04348

3 1 100 . -0.09091

2 1 95 . -0.05000

1 1 90 . -0.05263

6 2 1200 1300 .

5 2 1125 1200 -0.06250

4 2 1050 1150 -0.06667

3 2 950 . -0.09524

2 2 1100 . 0.15789

1 2 1000 . -0.09091&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I need to do the following:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;For observations where&amp;nbsp;mt not missing&amp;nbsp;calculate x=lag(mt)*(1+g)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;but&amp;nbsp;for observations where mt is missing use x=lag(x)*(1+g)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;And start again for each state.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;So I think I need to retain the values for x.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;Note that in practice I will have many observation per state.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;Thanks,&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 22:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472637#M285817</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-06-22T22:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from the previous row to do calculations on current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472640#M285818</link>
      <description>&lt;P&gt;Might help to&amp;nbsp; share the entire data step you are currently attempting. Also describe what is not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll go on a limb an guess that you have some code like&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if not missing(mt) then x=lag(mt)*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;else&amp;nbsp; x=lag(x)*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;And the results look "funny". The issue would be the nature of the lag function and the queue it maintains. So LAG when used with if refers to the last time the IF was true.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Better is to create a temporary variable to always have the last value and then use that as needed.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Lmt=Lag(mt);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if not missing(mt) then x=Lmt*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;else&amp;nbsp; x=Lmt*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;drop lmt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;&lt;FONT size="2"&gt;Since it appears that you should be using a BY state or similar then you could interrupt that use using FIRST. processing:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Lmt=Lag(mt);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if first.state then lmt=.; /*would prevent the first record of a state from using the previous value of mt.*/&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if not missing(mt) then x=Lmt*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;else&amp;nbsp; x=Lmt*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;drop lmt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need a special value of x for the first record of a state then add a do block:&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if first.state then do;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&amp;nbsp;&amp;nbsp; x= &amp;lt;something&amp;gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Else &lt;FONT size="2"&gt;if not missing(mt) then x=Lmt*(1+g);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;else&amp;nbsp; x=Lmt*(1+g);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 23:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472640#M285818</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-22T23:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from the previous row to do calculations on current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472643#M285819</link>
      <description>Thank you. Getting closer. But not all records are being calculated.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Here is the sample code:&lt;BR /&gt;&lt;BR /&gt;/* Sample dataset */&lt;BR /&gt;data temp1;&lt;BR /&gt;input month state ts mt;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 90 .&lt;BR /&gt;2 1 95 .&lt;BR /&gt;3 1 100 .&lt;BR /&gt;4 1 110 115&lt;BR /&gt;5 1 115 120&lt;BR /&gt;6 1 120 130&lt;BR /&gt;1 2 1000 .&lt;BR /&gt;2 2 1100 .&lt;BR /&gt;3 2 950 .&lt;BR /&gt;4 2 1050 1150&lt;BR /&gt;5 2 1125 1200&lt;BR /&gt;6 2 1200 1300&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* Sort by state and descending month */&lt;BR /&gt;proc sort data=temp1 out=temp2;&lt;BR /&gt;by state descending month;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp2;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* TS growth rates */&lt;BR /&gt;data temp3;&lt;BR /&gt;set temp2;&lt;BR /&gt;by state descending month;&lt;BR /&gt;&lt;BR /&gt;g=ts/lag(ts)-1;&lt;BR /&gt;if first.state then g=.;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp3;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;/* Backfill MT series */&lt;BR /&gt;data temp4;&lt;BR /&gt;set temp3;&lt;BR /&gt;by state descending month;&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;retain x;&lt;BR /&gt;if first.state then x=.;&lt;BR /&gt;if mt ne . then x=mt;&lt;BR /&gt;else if mt eq . then x=lag(x)*(1+g);&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;retain mt2;&lt;BR /&gt;if first.state then mt2=.;&lt;BR /&gt;mt2=min(lag(mt2)*(1+g_ts),mt);&lt;BR /&gt;*/&lt;BR /&gt;&lt;BR /&gt;Lmt=Lag(mt);&lt;BR /&gt;if not missing(mt) then x=Lmt*(1+g);&lt;BR /&gt;else x=Lmt*(1+g);&lt;BR /&gt;&lt;BR /&gt;if mt ne . then x=mt;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=temp4;&lt;BR /&gt;format x best12.;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;This is the output from the last print-x is null for latter records.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Obs month state ts mt g lmt x&lt;BR /&gt;&lt;BR /&gt;1 6 1 120 130 . . 130&lt;BR /&gt;2 5 1 115 120 -0.04167 130 120&lt;BR /&gt;3 4 1 110 115 -0.04348 120 115&lt;BR /&gt;4 3 1 100 . -0.09091 115 104.54545455&lt;BR /&gt;5 2 1 95 . -0.05000 . .&lt;BR /&gt;6 1 1 90 . -0.05263 . .&lt;BR /&gt;7 6 2 1200 1300 . . 1300&lt;BR /&gt;8 5 2 1125 1200 -0.06250 1300 1200&lt;BR /&gt;9 4 2 1050 1150 -0.06667 1200 1150&lt;BR /&gt;10 3 2 950 . -0.09524 1150 1040.4761905&lt;BR /&gt;11 2 2 1100 . 0.15789 . .&lt;BR /&gt;12 1 2 1000 . -0.09091 . .&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 22 Jun 2018 23:33:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472643#M285819</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-06-22T23:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from the previous row to do calculations on current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472672#M285820</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159527"&gt;@su17&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below based on your description and stuff I've seen in the code you've posted. Does that give you the result you're after? If not then what's missing/incorrect?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sample dataset */
data temp1;
  input month state ts mt;
  datalines;
1 1 90 .
2 1 95 .
3 1 100 .
4 1 110 115
5 1 115 120
6 1 120 130
1 2 1000 .
2 2 1100 .
3 2 950 .
4 2 1050 1150
5 2 1125 1200
6 2 1200 1300
;
run;

proc sort data=temp1 out=inter;
  by state DESCENDING month;
run;


/*For observations where mt not missing calculate x=lag(mt)*(1+g)*/
/*but for observations where mt is missing use x=lag(x)*(1+g)*/
/*g=ts/lag(ts)-1;*/
/*And start again for each state.*/
data want;
  set inter;
  by state DESCENDING month;
  retain x 0;
  x=coalesce(lag(mt),x)*(1+(ts/lag(ts)-1));
  if first.state then call missing(x);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Jun 2018 04:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472672#M285820</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-06-23T04:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to retain a value from the previous row to do calculations on current row?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472804#M285821</link>
      <description>&lt;P&gt;Yes! That is what I was looking for. Thanks very much!&lt;/P&gt;</description>
      <pubDate>Sun, 24 Jun 2018 18:17:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-retain-a-value-from-the-previous-row-to-do-calculations/m-p/472804#M285821</guid>
      <dc:creator>su17</dc:creator>
      <dc:date>2018-06-24T18:17:07Z</dc:date>
    </item>
  </channel>
</rss>

