<?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 of a computed variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540385#M149055</link>
    <description>&lt;P&gt;Please provide example data and expected output for that data.&lt;/P&gt;</description>
    <pubDate>Tue, 05 Mar 2019 10:05:48 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-03-05T10:05:48Z</dc:date>
    <item>
      <title>LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540382#M149054</link>
      <description>&lt;P&gt;I have a data set with variable a. I need to create variable b that is equal in line 1 to (1+a) and in each of the following lines to&amp;nbsp;the value created for b in the previous line multiplied by (1+a) (a of the current line). I tried different things and each time run into another problem. The problem is how ot get the lag of variable b I just computed in the previous line. I tried using retain, and computing a lag variable outside the if statement, and it still does not work. For example:&lt;/P&gt;&lt;P&gt;data test2;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;retain b;&lt;/P&gt;&lt;P&gt;lagb = lag(b);&lt;/P&gt;&lt;P&gt;if _N_ = 1 then b = sum(1,a);&lt;/P&gt;&lt;P&gt;else b= lagb *&amp;nbsp; sum(1,a);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 09:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540382#M149054</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2019-03-05T09:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540385#M149055</link>
      <description>&lt;P&gt;Please provide example data and expected output for that data.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 10:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540385#M149055</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-03-05T10:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540390#M149060</link>
      <description>&lt;P&gt;To continue with that approach you would need two datasteps, one to calculate the variable, the second to do the lag.&amp;nbsp; You cannot use lag() and variables which are not fixed.&amp;nbsp; There are other ways, such as retain, and merging on obs=obs+1.&amp;nbsp; However without test data in the form of a datastep we cannot provide anything.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 10:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540390#M149060</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-05T10:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540399#M149066</link>
      <description>Depending on your objective here, it is possible that a very simple program will do.  Try:&lt;BR /&gt;&lt;BR /&gt;data test2;&lt;BR /&gt;set test;&lt;BR /&gt;retain b 1;&lt;BR /&gt;b = b * sum(1, a);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;If that gives you the right result, great.  If not, you can still get the lag you are asking for.  Use:&lt;BR /&gt;&lt;BR /&gt;data test2:&lt;BR /&gt;lagb=b;&lt;BR /&gt;set test;&lt;BR /&gt;retain b;&lt;BR /&gt;if _n_=1 then b=sum(1, a);&lt;BR /&gt;else b=lagb * sum(1, a);&lt;BR /&gt;run;</description>
      <pubDate>Tue, 05 Mar 2019 10:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540399#M149066</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-05T10:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540402#M149068</link>
      <description>&lt;P&gt;Thank you for your replys. I simplified the data. input data:&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;8&lt;/P&gt;&lt;P&gt;5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;required output:&lt;/P&gt;&lt;P&gt;a&amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;10&amp;nbsp; 11&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; 33&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp;165&lt;/P&gt;&lt;P&gt;8&amp;nbsp; 1485&lt;/P&gt;&lt;P&gt;5&amp;nbsp; 8910&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;As explained above, b is equal in line 1 to (1+a) and in each of the following lines to&amp;nbsp;the value created for b in the previous line multiplied by (1+a) (a of the current line).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 11:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540402#M149068</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2019-03-05T11:06:39Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540406#M149071</link>
      <description>&lt;P&gt;Thank you! Looks like the first code you wrote does the job.&lt;/P&gt;&lt;P&gt;I see what you did with the retain b and using that instead of lag.&lt;/P&gt;&lt;P&gt;Does the retaon 1 retain line 1?&lt;/P&gt;&lt;P&gt;Thank you again!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 11:13:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540406#M149071</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2019-03-05T11:13:36Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540466#M149101</link>
      <description>&lt;P&gt;This statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;retain b 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;both retains B and gives it an initial value of 1&amp;nbsp; "Initial value" means at the very beginning of the DATA step, before reading in any of the data.&amp;nbsp; It does NOT mean at the beginning of each observation.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 15:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540466#M149101</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-05T15:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: LAG of a computed variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540765#M149228</link>
      <description>&lt;P&gt;Thank you for all your help!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 13:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-of-a-computed-variable/m-p/540765#M149228</guid>
      <dc:creator>Taliah</dc:creator>
      <dc:date>2019-03-06T13:31:21Z</dc:date>
    </item>
  </channel>
</rss>

