<?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/414835#M101675</link>
    <description>&lt;P&gt;In what sense sorry.&amp;nbsp; Do you mean you want to hold the value from 10 rows before, or all the rows up to 10?&amp;nbsp; To do the first is pretty straight forward, just add a counter, the second is a bit more coding, have an array and retain that:&lt;/P&gt;
&lt;PRE&gt;array l_reason{10};
retain l_reason:;&lt;/PRE&gt;
&lt;P&gt;Then keep an index on that.&amp;nbsp; Supplying test data (in a datastep) and required output really shows a problem simply!&lt;/P&gt;</description>
    <pubDate>Mon, 20 Nov 2017 13:54:59 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-11-20T13:54:59Z</dc:date>
    <item>
      <title>LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414808#M101665</link>
      <description>&lt;P&gt;Hi, I've been trying to do something fairly simple for a couple of hours, but am struggling.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've read that using LAG function can lead to some unexpected results, but I'm not sure what I'm doing that could be causing the issue.&lt;/P&gt;&lt;P&gt;I am trying to create the dataset below:&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;Day End_Reason                Prev_End_Reason Last_Day
1    Reason 1                  ""                0
2    Reason 1                  ""                0
3    Reason 1                  ""                1
4    Reason 2                  Reason 1          0
5    Reason 2                  Reason 1          0
6    Reason 2                  Reason 1          1
7    Reason 2                  Reason 2          1
8    Reason 2                  Reason 2          0
9    Reason 2                  Reason 2          0
10   Reason 2                  Reason 2          1
11   Reason 3                  Reason 2          0
12   Reason 3                  Reason 2          0
13   Reason 3                  Reason 2          0
14   Reason 3                  Reason 2          1
15   ""                        Reason 3          0&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, each row has an "End_Reason". If the previous row has Last_Day =1&amp;nbsp; would like the following observation to be&amp;nbsp;have "Previous_End_Reason" = "End_Reason" from the previous row.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I currently have the above, excluding the "Prev_end_reason" variable, which I am trying to attain.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code currently looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;	
	if lag(last_day) = 1 then prev_end_reason = lag(end_reason);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;For some reason, the column "Prev_End_Reason" remains blank throughout. If I use the following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;	
	if lag(last_day) = 1 then prev_end_reason = 10;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It seems to work, putting a "10" in the correct places, so I can only assume there is something wrong with the bit "lag(end_reason)", but I can't work out what?&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;&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;&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;&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;&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>Mon, 20 Nov 2017 11:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414808#M101665</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-11-20T11:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414811#M101667</link>
      <description>&lt;P&gt;Never use lag() in a conditional branch. Lag() sets up a FIFO chain that is only filled when lag() is actually called. Will test-run your code next time I sit in front of my SAS (max. 1 hour).&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 12:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414811#M101667</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-20T12:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414814#M101668</link>
      <description>&lt;P&gt;Yes, I only ever use lag() as a lookup function, I always use retained variables for anything else.&amp;nbsp; So I would do:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain lst_day lst_reason;	
  if lst_day=1 then prev_end_reason = lst_reason;
  lst_day=last_day;
  lst_reason=prev_end_reason;
run;;&lt;/PRE&gt;
&lt;P&gt;Note I haven't tested this - provide test data&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;in the form of a datastep and what you want out.&lt;/STRONG&gt;&lt;/U&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 12:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414814#M101668</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-20T12:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414819#M101671</link>
      <description>Hey, thank you! That worked! Just changed the code slightly to:&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;retain lst_day lst_reason;&lt;BR /&gt;if lst_day=1 then prev_end_reason = lst_reason;&lt;BR /&gt;lst_day=last_day;&lt;BR /&gt;lst_reason=end_reason;&lt;BR /&gt;run;&lt;BR /&gt;Many thanks!&lt;BR /&gt;&lt;BR /&gt;Quick question, how would you do this if you wanted to "lag", say, 10 rows?</description>
      <pubDate>Mon, 20 Nov 2017 12:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414819#M101671</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-11-20T12:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: LAG function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414835#M101675</link>
      <description>&lt;P&gt;In what sense sorry.&amp;nbsp; Do you mean you want to hold the value from 10 rows before, or all the rows up to 10?&amp;nbsp; To do the first is pretty straight forward, just add a counter, the second is a bit more coding, have an array and retain that:&lt;/P&gt;
&lt;PRE&gt;array l_reason{10};
retain l_reason:;&lt;/PRE&gt;
&lt;P&gt;Then keep an index on that.&amp;nbsp; Supplying test data (in a datastep) and required output really shows a problem simply!&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 13:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-function/m-p/414835#M101675</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-20T13:54:59Z</dc:date>
    </item>
  </channel>
</rss>

