<?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 How to lookup previous value of a raw to calculate new value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353292#M82457</link>
    <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a sasdataset that looks like this. I need to recalculate the total column using values from the previous month and ratio from the current month. I tried using lag and retain function but I couldn’t get the correct answer. Any suggestion?&lt;/P&gt;
&lt;P&gt;SAS dataset;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; t1;&lt;/P&gt;
&lt;P&gt;input group $ amt ratio month total;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;A 1200 0.2 1 240&lt;/P&gt;
&lt;P&gt;B 3290 0.1 2 0&lt;/P&gt;
&lt;P&gt;C 2000 0.1 3 0&lt;/P&gt;
&lt;P&gt;A 1000 0.4 1 240&lt;/P&gt;
&lt;P&gt;B 5000 0.5 2 0&lt;/P&gt;
&lt;P&gt;C 1430 0.6 3 0&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; t2;&lt;/P&gt;
&lt;P&gt;set t1;&lt;/P&gt;
&lt;P&gt;if month gt &lt;STRONG&gt;2&lt;/STRONG&gt; then do;&lt;/P&gt;
&lt;P&gt;total = ratio * lag(total);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;retain total;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;desire results:&lt;/P&gt;
&lt;P&gt;A 1200 0.2 1 240&lt;/P&gt;
&lt;P&gt;B 3290 0.1 2 24&lt;/P&gt;
&lt;P&gt;C 2000 0.1 3 2.4&lt;/P&gt;
&lt;P&gt;A 1000 0.4 1 240&lt;/P&gt;
&lt;P&gt;B 5000 0.5 2 120&lt;/P&gt;
&lt;P&gt;C 1430 0.6 3 72&lt;/P&gt;</description>
    <pubDate>Tue, 25 Apr 2017 16:48:45 GMT</pubDate>
    <dc:creator>zqkal</dc:creator>
    <dc:date>2017-04-25T16:48:45Z</dc:date>
    <item>
      <title>How to lookup previous value of a raw to calculate new value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353292#M82457</link>
      <description>&lt;P&gt;Hi everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a sasdataset that looks like this. I need to recalculate the total column using values from the previous month and ratio from the current month. I tried using lag and retain function but I couldn’t get the correct answer. Any suggestion?&lt;/P&gt;
&lt;P&gt;SAS dataset;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; t1;&lt;/P&gt;
&lt;P&gt;input group $ amt ratio month total;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;A 1200 0.2 1 240&lt;/P&gt;
&lt;P&gt;B 3290 0.1 2 0&lt;/P&gt;
&lt;P&gt;C 2000 0.1 3 0&lt;/P&gt;
&lt;P&gt;A 1000 0.4 1 240&lt;/P&gt;
&lt;P&gt;B 5000 0.5 2 0&lt;/P&gt;
&lt;P&gt;C 1430 0.6 3 0&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; t2;&lt;/P&gt;
&lt;P&gt;set t1;&lt;/P&gt;
&lt;P&gt;if month gt &lt;STRONG&gt;2&lt;/STRONG&gt; then do;&lt;/P&gt;
&lt;P&gt;total = ratio * lag(total);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;retain total;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;desire results:&lt;/P&gt;
&lt;P&gt;A 1200 0.2 1 240&lt;/P&gt;
&lt;P&gt;B 3290 0.1 2 24&lt;/P&gt;
&lt;P&gt;C 2000 0.1 3 2.4&lt;/P&gt;
&lt;P&gt;A 1000 0.4 1 240&lt;/P&gt;
&lt;P&gt;B 5000 0.5 2 120&lt;/P&gt;
&lt;P&gt;C 1430 0.6 3 72&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2017 16:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353292#M82457</guid>
      <dc:creator>zqkal</dc:creator>
      <dc:date>2017-04-25T16:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to lookup previous value of a raw to calculate new value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353298#M82459</link>
      <description>&lt;P&gt;Looking at your desired data output, I'm guessing that this has to happen only for Group B and C and not when the group is equal to A?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2017 16:55:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353298#M82459</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-25T16:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to lookup previous value of a raw to calculate new value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353321#M82466</link>
      <description>&lt;P&gt;You can't calculate LAG conditionally.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Calculate lag outside of the IF condition and then you can use it in the IF condition.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2017 17:30:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353321#M82466</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-25T17:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to lookup previous value of a raw to calculate new value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353329#M82468</link>
      <description>&lt;P&gt;Here's a way that should work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data t2;&lt;/P&gt;
&lt;P&gt;prior_total = total;&lt;/P&gt;
&lt;P&gt;set t1;&lt;/P&gt;
&lt;P&gt;if month &amp;gt;&amp;nbsp;1 then total = ratio * prior_total;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A couple of notes ... you used gt as the comparison.&amp;nbsp; But the results are consistent with ge, not gt (hence month &amp;gt; 1 in my code).&amp;nbsp; Also, there is no need to retain TOTAL.&amp;nbsp; Since it is part of an incoming SAS data set, it is automatically retained.&amp;nbsp; It's up to you if you want to drop PRIOR_TOTAL.&amp;nbsp; You might want to keep it the first time, to better understand how the program works.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2017 17:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353329#M82468</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-25T17:35:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to lookup previous value of a raw to calculate new value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353343#M82475</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t1;
input group $ amt ratio month total;
datalines;
A 1200 0.2 1 240
B 3290 0.1 2 0
C 2000 0.1 3 0
A 1000 0.4 1 240
B 5000 0.5 2 0
C 1430 0.6 3 0
;
run; 

data t2;
set t1_NEW; 
FORMAT NEWTOTAL 8.2;
retain NEWTOTAL; 
IF MONTH = 1 THEN DO;
NEWTOTAL = TOTAL;
END;
if month GE 2 then do;
NEWTOTAL = ratio * NEWTOTAL;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Edit: Sorry, forgot the final run command!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2017 17:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-lookup-previous-value-of-a-raw-to-calculate-new-value/m-p/353343#M82475</guid>
      <dc:creator>thomp7050</dc:creator>
      <dc:date>2017-04-25T17:53:07Z</dc:date>
    </item>
  </channel>
</rss>

