<?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: Multiply by lag value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684355#M207377</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/260675"&gt;@abdulla&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are the victim, as so many SAS users, of the misleading naming of the lag function.&amp;nbsp; If really should be named UFQ (update fifo queue), because that's what it is.&amp;nbsp; So your statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else kc = 0.8* lag(kc) +rd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is NOT always fetching the prior value of KC.&amp;nbsp; Instead it is fetching the oldest value put in the queue (a queue of length one in this case, so it's also the ONLY value in the queue).&amp;nbsp; And since the queue was not updated at the start of each gvkey, the second obs of each gvkey was really retrieving the kc value from the end of the prior gvkey - because that's the last time the queue was updated, i.e. the last time the "lag" function was executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need a way to always update the queue, but not always use its value.&amp;nbsp; Use the IFN function as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;kd=ifn(first.gvkey,kc,0.8*lag(kc)+rd);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead of the two statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.gvkey then kc=rd;
else kc = 0.8* lag(kc) +rd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works because the IFN function ALWAYS executes both outcomes (i.e. 2nd and 3rd arguments) before it chooses a result based on the first argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW there are folks who will tell you to never use the lag function in an IF statement.&amp;nbsp; I disagree - it's a great way to track interleaved series.&amp;nbsp;&amp;nbsp; Say you you have two classes of KC, indicated by class='A' or class='B'.&amp;nbsp; And you actually want, for each A the most recent prior KC for class A, and the same for class B.&amp;nbsp; Then an IF statement is just the thing for the lag function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input class :$1.  kc @@;
datalines;
A 11   B 91   B 92   A 12   B 93   
B 94   A 13   A 14   B 95   A 15
run;
data want;
  set have;
  if class='A' then lastkc=lag(kc); else
  if class='B' then lastkc=lag(kc);
  put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which produces&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;class=A kc=11 lastkc=.
class=B kc=91 lastkc=.
class=B kc=92 lastkc=91
class=A kc=12 lastkc=11
class=B kc=93 lastkc=92
class=B kc=94 lastkc=93
class=A kc=13 lastkc=12
class=A kc=14 lastkc=13
class=B kc=95 lastkc=94
class=A kc=15 lastkc=14
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So notice all the A's have lagged value in the teens (i.e. only values for class A), and all the B's have lagged values in the 90's&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Sep 2020 19:45:10 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-09-16T19:45:10Z</dc:date>
    <item>
      <title>Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684345#M207370</link>
      <description>&lt;TABLE&gt;&lt;T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6.58&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1982&lt;/TD&gt;&lt;TD&gt;1.8&lt;/TD&gt;&lt;TD&gt;7.06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1983&lt;/TD&gt;&lt;TD&gt;12.45&lt;/TD&gt;&lt;TD&gt;18.10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1984&lt;/TD&gt;&lt;TD&gt;15.55&lt;/TD&gt;&lt;TD&gt;30.03&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;TD&gt;1985&lt;/TD&gt;&lt;TD&gt;16.87&lt;/TD&gt;&lt;TD&gt;40.89&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi, From the above data, I want to calculate values in the "Desired Value" column.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired value = 0.8*lag(kc)+rd.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here, for each of the first observation according to gvkey, there is no kc. So, for the first gvkey the equation will be&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired value = 0.8*0+rd. That means, the first gvkey kc is zero. I used the following code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by gvkey fyear;&lt;/P&gt;&lt;P&gt;retain kc;&lt;/P&gt;&lt;P&gt;if first.gvkey then kc=rd;&lt;/P&gt;&lt;P&gt;else kc = 0.8* lag(kc) +rd;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Problem is that I am not getting the desired result. Could you please help me? What mistakes am I making?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 18:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684345#M207370</guid>
      <dc:creator>abdulla</dc:creator>
      <dc:date>2020-09-16T18:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684347#M207372</link>
      <description>&lt;P&gt;If you use the LAG function inside and IF statement, it doesn't behave the way you expect it to behave. So, I remove the LAG from the IF, and now it should work properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by gvkey;
retain kc;
prev_kc=lag(kc);
if first.gvkey then kc=rd;
else kc=prev_kc*0.80 + rd;
drop prev_kc;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is untested code. If you want tested code, please &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;provide data as a SAS data step&lt;/A&gt; and not as a screen capture.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 19:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684347#M207372</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-09-16T19:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684348#M207373</link>
      <description>&lt;P&gt;Each call to the LAG or DIF function creates a separate queue of values. Which means when you place one of those functions in a conditional statement the value is from the last time the condition was true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you are RETAINing the value of KC you do not need lag though. This yields your desired KC values (less some rounding)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data want;
   set have;
   by gvkey fyear;
   retain kc;
   if first.gvkey then kc=rd;
   else kc = 0.8* kc +rd;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Sep 2020 19:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684348#M207373</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-16T19:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684353#M207375</link>
      <description>Thank you very much.</description>
      <pubDate>Wed, 16 Sep 2020 19:38:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684353#M207375</guid>
      <dc:creator>abdulla</dc:creator>
      <dc:date>2020-09-16T19:38:47Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684354#M207376</link>
      <description>Thank you very much</description>
      <pubDate>Wed, 16 Sep 2020 19:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684354#M207376</guid>
      <dc:creator>abdulla</dc:creator>
      <dc:date>2020-09-16T19:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684355#M207377</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/260675"&gt;@abdulla&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are the victim, as so many SAS users, of the misleading naming of the lag function.&amp;nbsp; If really should be named UFQ (update fifo queue), because that's what it is.&amp;nbsp; So your statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;else kc = 0.8* lag(kc) +rd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is NOT always fetching the prior value of KC.&amp;nbsp; Instead it is fetching the oldest value put in the queue (a queue of length one in this case, so it's also the ONLY value in the queue).&amp;nbsp; And since the queue was not updated at the start of each gvkey, the second obs of each gvkey was really retrieving the kc value from the end of the prior gvkey - because that's the last time the queue was updated, i.e. the last time the "lag" function was executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need a way to always update the queue, but not always use its value.&amp;nbsp; Use the IFN function as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;kd=ifn(first.gvkey,kc,0.8*lag(kc)+rd);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead of the two statements:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.gvkey then kc=rd;
else kc = 0.8* lag(kc) +rd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works because the IFN function ALWAYS executes both outcomes (i.e. 2nd and 3rd arguments) before it chooses a result based on the first argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW there are folks who will tell you to never use the lag function in an IF statement.&amp;nbsp; I disagree - it's a great way to track interleaved series.&amp;nbsp;&amp;nbsp; Say you you have two classes of KC, indicated by class='A' or class='B'.&amp;nbsp; And you actually want, for each A the most recent prior KC for class A, and the same for class B.&amp;nbsp; Then an IF statement is just the thing for the lag function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input class :$1.  kc @@;
datalines;
A 11   B 91   B 92   A 12   B 93   
B 94   A 13   A 14   B 95   A 15
run;
data want;
  set have;
  if class='A' then lastkc=lag(kc); else
  if class='B' then lastkc=lag(kc);
  put (_all_) (=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which produces&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;class=A kc=11 lastkc=.
class=B kc=91 lastkc=.
class=B kc=92 lastkc=91
class=A kc=12 lastkc=11
class=B kc=93 lastkc=92
class=B kc=94 lastkc=93
class=A kc=13 lastkc=12
class=A kc=14 lastkc=13
class=B kc=95 lastkc=94
class=A kc=15 lastkc=14
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So notice all the A's have lagged value in the teens (i.e. only values for class A), and all the B's have lagged values in the 90's&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 19:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684355#M207377</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-16T19:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684384#M207386</link>
      <description>&lt;P&gt;But UFQ is so hard to pronounce!&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 21:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684384#M207386</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-16T21:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: Multiply by lag value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684432#M207404</link>
      <description>&lt;P&gt;How about QUP?&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&amp;nbsp;&amp;nbsp; or UPQ?&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Sep 2020 00:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiply-by-lag-value/m-p/684432#M207404</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-09-17T00:10:21Z</dc:date>
    </item>
  </channel>
</rss>

