<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897651#M354739</link>
    <description>&lt;P&gt;There's a good argument to be made that the LAG function is misleadingly named.&amp;nbsp; Folks familiar with spreadsheets, for example, just think of the LAG function as a way to look back 1 row (or 2 rows for a 2-period lag, etc.).&amp;nbsp; So you might naturally expect that the syntax below, using an IF ... THEN ...&amp;nbsp; ELSE construct.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.id then y=.;
  else y=lag(x);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for all other functions, but will yield unexpected results for the lag function (and the related dif function).&amp;nbsp; You will discover that the second obs for all id's but the first id will get a Y value taken from the last obs of the preceding ID, rather than from the first obs of the same ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's because the unfortunately named &lt;EM&gt;&lt;STRONG&gt;LAG function is not a lookback&lt;/STRONG&gt;&lt;/EM&gt; - it is a &lt;EM&gt;&lt;STRONG&gt;FIFO queue update&lt;/STRONG&gt;&lt;/EM&gt; (a queue of length 1 in this case).&amp;nbsp; Think of the queue as a &lt;EM&gt;&lt;STRONG&gt;retained value&lt;/STRONG&gt;&lt;/EM&gt; (or retained &lt;EM&gt;&lt;STRONG&gt;values&lt;/STRONG&gt;&lt;/EM&gt; for lag2+).&amp;nbsp; For most purposes, including this task, that queue of retained value(s) has to be updated for every observation even if the value in the queue is not going to be used.&amp;nbsp; So many folks use this 4-statement structure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lx = lag(x);
if first.id) then y=.;
else y=lx;
drop lx;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can instead do the above in one statement using the IFN (or IFC) function, as in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y=ifn(first.id,.,lag(x));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works because the &lt;EM&gt;&lt;STRONG&gt;IFN always evaluates both the second and third arguments&lt;/STRONG&gt;&lt;/EM&gt;, regardless of whether the first argument is true or false.&amp;nbsp; The lag-generated queue is therefore always updated, as is needed in this case.&lt;/P&gt;</description>
    <pubDate>Sat, 07 Oct 2023 00:28:11 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-10-07T00:28:11Z</dc:date>
    <item>
      <title>lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897618#M354720</link>
      <description>&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;data svn;&lt;BR /&gt;input id sv vt ;&lt;BR /&gt;informat sv vt date9.;&lt;BR /&gt;cards ;&lt;BR /&gt;101 01jan2020 31jan2020&lt;BR /&gt;101 15feb2020 28feb2020&lt;BR /&gt;101 11mar2020 31mar2020&lt;BR /&gt;102 01apr2020 30apr2020&lt;BR /&gt;102 18may2020 31may2020&lt;BR /&gt;102 22sep2020 30sep2020&lt;BR /&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;From using above data i want below mentioned output :&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; SV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; VT&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lag&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tot&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; 01jan2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 31jan2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15feb2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 28feb2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31JAN2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11mar2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31mar2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 28FEB2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01apr2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 30apr2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;18may2020&amp;nbsp; &amp;nbsp; &amp;nbsp;31may2020&amp;nbsp; &amp;nbsp; &amp;nbsp;30APR2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 18 &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;102&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22sep2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 30sep2020&amp;nbsp; &amp;nbsp; &amp;nbsp; 31MAY2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 114&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 17:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897618#M354720</guid>
      <dc:creator>112211</dc:creator>
      <dc:date>2023-10-06T17:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897624#M354724</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set svn;
by id;
lag = lag(vt);
if not first.id
then tot = sv - lag;
else lag = .;
format lag date9.;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 06 Oct 2023 17:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897624#M354724</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-10-06T17:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897651#M354739</link>
      <description>&lt;P&gt;There's a good argument to be made that the LAG function is misleadingly named.&amp;nbsp; Folks familiar with spreadsheets, for example, just think of the LAG function as a way to look back 1 row (or 2 rows for a 2-period lag, etc.).&amp;nbsp; So you might naturally expect that the syntax below, using an IF ... THEN ...&amp;nbsp; ELSE construct.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.id then y=.;
  else y=lag(x);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for all other functions, but will yield unexpected results for the lag function (and the related dif function).&amp;nbsp; You will discover that the second obs for all id's but the first id will get a Y value taken from the last obs of the preceding ID, rather than from the first obs of the same ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's because the unfortunately named &lt;EM&gt;&lt;STRONG&gt;LAG function is not a lookback&lt;/STRONG&gt;&lt;/EM&gt; - it is a &lt;EM&gt;&lt;STRONG&gt;FIFO queue update&lt;/STRONG&gt;&lt;/EM&gt; (a queue of length 1 in this case).&amp;nbsp; Think of the queue as a &lt;EM&gt;&lt;STRONG&gt;retained value&lt;/STRONG&gt;&lt;/EM&gt; (or retained &lt;EM&gt;&lt;STRONG&gt;values&lt;/STRONG&gt;&lt;/EM&gt; for lag2+).&amp;nbsp; For most purposes, including this task, that queue of retained value(s) has to be updated for every observation even if the value in the queue is not going to be used.&amp;nbsp; So many folks use this 4-statement structure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lx = lag(x);
if first.id) then y=.;
else y=lx;
drop lx;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can instead do the above in one statement using the IFN (or IFC) function, as in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y=ifn(first.id,.,lag(x));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works because the &lt;EM&gt;&lt;STRONG&gt;IFN always evaluates both the second and third arguments&lt;/STRONG&gt;&lt;/EM&gt;, regardless of whether the first argument is true or false.&amp;nbsp; The lag-generated queue is therefore always updated, as is needed in this case.&lt;/P&gt;</description>
      <pubDate>Sat, 07 Oct 2023 00:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag/m-p/897651#M354739</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-07T00:28:11Z</dc:date>
    </item>
  </channel>
</rss>

