<?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: Unexpected results with lag function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820750#M324004</link>
    <description>&lt;P&gt;You never passed in the date from the first observation per ID into the queue/stack that LAG() uses.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it is not there to be returned later.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1656438561790.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72782i14D0191D97E44919/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1656438561790.png" alt="Tom_0-1656438561790.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;In general you never want to conditionally run the LAG() function because of this issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by ID;
   prev_event_dt=lag(Event_Date);
   if first.ID then prev_event_dt = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jun 2022 17:49:56 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-06-28T17:49:56Z</dc:date>
    <item>
      <title>Unexpected results with lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820747#M324001</link>
      <description>&lt;P&gt;I am getting unusual results when I am using the lag function and not getting the results I am looking for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Event_Date&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;12MAY2020&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 24JUN2020&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 08AUG2020&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 09APR2020&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 26JUN2020&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 30AUG2020&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Program I wrote:&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if first.ID then prev_event_dt = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else prev_event_dt=lag(Event_Date);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output I am expecting:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Event_Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;prev_event_dt&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;12MAY2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 24JUN2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12MAY2020&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 08AUG2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;24JUN2020&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 09APR2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 26JUN2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 30AUG2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;26JUN2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please point out where I am going wrong with the program I wrote.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 17:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820747#M324001</guid>
      <dc:creator>NickS2</dc:creator>
      <dc:date>2022-06-28T17:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected results with lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820749#M324003</link>
      <description>&lt;P&gt;LAG does not behave as you expect it in IF / THEN / ELSE statements. (It's doing the right thing, but your expectation is wrong)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Better you should use the &lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n0l3n5z2h31h7wn1fmnqd33ibhap.htm" target="_self"&gt;IFN function&lt;/A&gt; (or IFC if you want to create a character variable)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;prev_event_dt=ifn(first.id,.,lag(event_date));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 17:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820749#M324003</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-28T17:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected results with lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820750#M324004</link>
      <description>&lt;P&gt;You never passed in the date from the first observation per ID into the queue/stack that LAG() uses.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So it is not there to be returned later.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1656438561790.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72782i14D0191D97E44919/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1656438561790.png" alt="Tom_0-1656438561790.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;In general you never want to conditionally run the LAG() function because of this issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by ID;
   prev_event_dt=lag(Event_Date);
   if first.ID then prev_event_dt = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jun 2022 17:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820750#M324004</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-28T17:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: Unexpected results with lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820751#M324005</link>
      <description>&lt;P&gt;The LAG function creates a FIFO queue that is filled every time the function is called. Calling LAG in a conditional branch means that not all values will make it into the queue.&lt;/P&gt;
&lt;P&gt;Restructure your code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by ID;
prev_event_dt = lag(Event_Date);
if first.ID then prev_event_dt = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jun 2022 17:42:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unexpected-results-with-lag-function/m-p/820751#M324005</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-28T17:42:13Z</dc:date>
    </item>
  </channel>
</rss>

