<?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 calculate a variable based on a lag logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895235#M353678</link>
    <description>&lt;P&gt;Many Thanks !&lt;/P&gt;</description>
    <pubDate>Thu, 21 Sep 2023 11:59:38 GMT</pubDate>
    <dc:creator>J111</dc:creator>
    <dc:date>2023-09-21T11:59:38Z</dc:date>
    <item>
      <title>How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895199#M353655</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please find below data Have and data Want.&lt;/P&gt;
&lt;P&gt;The index for the first observation is 1 and then it is based on the following logic&lt;/P&gt;
&lt;P&gt;index = lag(index) + rate/100/365 ;&lt;/P&gt;
&lt;P&gt;-------------------------------------&lt;BR /&gt;data Have ;&lt;BR /&gt;input obs rate ;&lt;BR /&gt;cards ;&lt;BR /&gt;1 0.35 &lt;BR /&gt;2 0.35 &lt;BR /&gt;3 0.35&lt;BR /&gt;4 0.35&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;----------------------------&lt;/P&gt;
&lt;P&gt;data Want ;&lt;BR /&gt;input obs index ;&lt;BR /&gt;cards ;&lt;BR /&gt;1 1 &lt;BR /&gt;2 1.00000959 &lt;BR /&gt;3 1.00001918&lt;BR /&gt;4 1.00002877&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;-----------------------------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My following code test was not good, pls adivse&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data next ;&lt;BR /&gt;set have ;&lt;BR /&gt;length Index 8 ;&lt;BR /&gt;retain index 1 ;&lt;BR /&gt;_value = lag(index) ;&lt;BR /&gt;if obs&amp;nbsp; gt 1&amp;nbsp; then index = _value + rate/100/365 ; &lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;--------------------&lt;/P&gt;
&lt;P&gt;This is originally from EXCEL function ( E2+C3/100/365 ) such that column E is the index and column C is the rate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 09:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895199#M353655</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2023-09-21T09:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895206#M353658</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps I have not understood what you're asking, but when I ran your code, the index column that you calculated appears to match your want data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amir_0-1695292171247.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88195i5C7BF0B50E6F3219/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Amir_0-1695292171247.png" alt="Amir_0-1695292171247.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Were you looking for something else?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 10:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895206#M353658</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2023-09-21T10:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895208#M353660</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;Each observation should get a different value of index.&lt;/P&gt;
&lt;P&gt;However, in the test code both observation 2 and 3 get the same value of index.&lt;/P&gt;
&lt;P&gt;This is wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 10:32:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895208#M353660</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2023-09-21T10:32:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895225#M353672</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="J111_1-1695295156366.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88197i106F280A8B8E1715/image-size/medium?v=v2&amp;amp;px=400" role="button" title="J111_1-1695295156366.png" alt="J111_1-1695295156366.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;A little clarification:&lt;/P&gt;
&lt;P&gt;The calculation above in Excel is correct.&lt;/P&gt;
&lt;P&gt;How can we do it in SAS ?&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 11:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895225#M353672</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2023-09-21T11:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895230#M353675</link>
      <description>&lt;P&gt;The problem is related to the fact that LAG is not a lookback function - it is an update FIFO queue function.&amp;nbsp; &amp;nbsp;And you are updating that queue in the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; _value = lag(index) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement prior to the recalculation of INDEX in the&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if obs  gt 1  then index = _value + rate/100/365 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;statement.&amp;nbsp; So the recalculated INDEX value does not get into the queue in a timely way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in this case, you don't need LAG at all, since you are retaining INDEX.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Simplify your code to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data next ;
  set have ;
  length Index 8 ;
  retain index 1 ;
  if obs gt 1 then index = index + rate/100/365;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 13:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895230#M353675</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-21T13:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate a variable based on a lag logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895235#M353678</link>
      <description>&lt;P&gt;Many Thanks !&lt;/P&gt;</description>
      <pubDate>Thu, 21 Sep 2023 11:59:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-a-variable-based-on-a-lag-logic/m-p/895235#M353678</guid>
      <dc:creator>J111</dc:creator>
      <dc:date>2023-09-21T11:59:38Z</dc:date>
    </item>
  </channel>
</rss>

