<?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 function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347989#M80516</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the LAG function has to be executed for every row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Find below the changed code. The LAG function is executed for every row, if we are on the first row of a group we reset the value for the variable x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t;
  input cust_id date:date9.  sales;

  format date date9.;
  cards;
001 23feb2017 100
001 22jan2016 200
001 23mar2017 300
002 01jun2015 200
002 03aug2016 300
002 05apr2016 400
;

data t1;
  set t;
  by cust_id;
  x =lag(date);

  if first.cust_id=1 then do;
    x = .;
  end;
  format x date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
    <pubDate>Fri, 07 Apr 2017 06:16:58 GMT</pubDate>
    <dc:creator>BrunoMueller</dc:creator>
    <dc:date>2017-04-07T06:16:58Z</dc:date>
    <item>
      <title>Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347988#M80515</link>
      <description>&lt;P&gt;I have this code&lt;/P&gt;&lt;P&gt;data t;&lt;/P&gt;&lt;P&gt;input cust_id date:date9. &amp;nbsp;sales;&lt;/P&gt;&lt;P&gt;001 23feb2017 100&lt;/P&gt;&lt;P&gt;001 22jan2016 200&lt;/P&gt;&lt;P&gt;001 23mar2017 300&lt;/P&gt;&lt;P&gt;002 01june2015 200&lt;/P&gt;&lt;P&gt;002 03aug2016 300&lt;/P&gt;&lt;P&gt;002 05april2016 400&lt;/P&gt;&lt;P&gt;;run;&lt;/P&gt;&lt;P&gt;data t1;&lt;/P&gt;&lt;P&gt;set t;&lt;/P&gt;&lt;P&gt;by cust_id;&lt;/P&gt;&lt;P&gt;if first.cust_id=0 then x =lag(date);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;I'm getting a missing vcalue in second observation, where I should get the date of first observation?&lt;/P&gt;&lt;P&gt;Except this it's working fine.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 06:06:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347988#M80515</guid>
      <dc:creator>pawandh</dc:creator>
      <dc:date>2017-04-07T06:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347989#M80516</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the LAG function has to be executed for every row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Find below the changed code. The LAG function is executed for every row, if we are on the first row of a group we reset the value for the variable x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t;
  input cust_id date:date9.  sales;

  format date date9.;
  cards;
001 23feb2017 100
001 22jan2016 200
001 23mar2017 300
002 01jun2015 200
002 03aug2016 300
002 05apr2016 400
;

data t1;
  set t;
  by cust_id;
  x =lag(date);

  if first.cust_id=1 then do;
    x = .;
  end;
  format x date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 06:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347989#M80516</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2017-04-07T06:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347993#M80520</link>
      <description>&lt;P&gt;To expand on &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32"&gt;@BrunoMueller&lt;/a&gt;'s answer: The lag() function is a First-In-First-Out queue, which is &lt;U&gt;only&lt;/U&gt; fed when the function is called. If you call it conditionally, everytime the condition is not met, the current value in the variable will not be fed into the queue. Since you did not call lag() in the first data step iteration, its content was still missing when you called it in the second iteration. You'll also get a wrong value after each first. condition, as you'll still have the value from the previous last. observation.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 06:46:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function/m-p/347993#M80520</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-07T06:46:21Z</dc:date>
    </item>
  </channel>
</rss>

