<?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: conditional lag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368791#M87972</link>
    <description>&lt;P&gt;The lag() function works like a FIFO (first-in-first-out) queue. It only gets a value when it is called. Calling it conditionally will result in not all values being fed into the queue, and therefore unwanted results.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jun 2017 15:41:17 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-06-20T15:41:17Z</dc:date>
    <item>
      <title>conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368778#M87969</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain me the lag fuction in terms of 'QUEUE'? &amp;nbsp;Also why conditional lag can give incorrect result?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.howles.com/saspapers/CC33.pdf" target="_blank"&gt;http://www.howles.com/saspapers/CC33.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks !&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:15:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368778#M87969</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-06-20T15:15:44Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368791#M87972</link>
      <description>&lt;P&gt;The lag() function works like a FIFO (first-in-first-out) queue. It only gets a value when it is called. Calling it conditionally will result in not all values being fed into the queue, and therefore unwanted results.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 15:41:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368791#M87972</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-20T15:41:17Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368827#M87978</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I wonder if you have an example for this?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 17:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368827#M87978</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-06-20T17:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368863#M87983</link>
      <description>&lt;P&gt;I leave that as an exercise for the reader. It's really simple to devise code that illustrates this.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/64404"&gt;@SAS_inquisitive&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;I wonder if you have an example for this?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 18:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368863#M87983</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-20T18:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368880#M87985</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;Thank you. Looks like this paper has really good examples.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.lexjansen.com/nesug/nesug10/cc/cc22.pdf" target="_blank"&gt;http://www.lexjansen.com/nesug/nesug10/cc/cc22.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jun 2017 18:55:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/368880#M87985</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-06-20T18:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369684#M88290</link>
      <description>&lt;P&gt;Let's I have a mock data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data mock;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;input x;&lt;/P&gt;&lt;P&gt;&amp;nbsp;cards;&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I execute lag(x), what would be the value in queue 3 or 1? Is there anyway we can see the values in queue?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks !&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 19:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369684#M88290</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-06-22T19:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369722#M88301</link>
      <description>&lt;P&gt;so here is my understanding, when x = 1, there is nothing in the queue, hence it returns missing value. When X=2, 1 is in the queue and it returns 1. Similarly when x =3, 2 is in the queue, and it returns a value of 2.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 22:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369722#M88301</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2017-06-22T22:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369728#M88304</link>
      <description>&lt;P&gt;Your understanding is correct.&amp;nbsp; I don't think there s a way to see what is inside the queue, other than pushing values into the queue to push them out.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Definitely take the time to play around with conditional lags, to convince yourself that the lag() function works when called conditionally.&amp;nbsp; Far too many people mistakenly think there is a bug in the lag function and that it should never be called conditionally. It works perfectly fine conditionally, you just need to understand how a queue works.&amp;nbsp; e.g., think about what you should expect from:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set mock;
  if x IN (2,3) then lag=lag(x);
  put x= lag=;
run;&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;Also worth playing with lags with longer queues, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set mock;
  lag1=lag1(x);
  lag2=lag2(x);
  lag3=lag3(x);
  put (x lag1 lag2 lag3)(=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 22:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369728#M88304</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2017-06-22T22:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: conditional lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369781#M88326</link>
      <description>&lt;P&gt;To slightly expand your code and show the problems of a conditional lag, just do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mock;
input x;
cards;
1
2
3
4
5
;
run;

data test;
set mock;
if x ne 3 then x1 = lag(x);
run;

proc print data=test noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result shows the effect of lag() not being called in obs 3:&lt;/P&gt;
&lt;PRE&gt;x    x1

1     .
2     1
3     .
4     2
5     4
&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jun 2017 06:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/conditional-lag/m-p/369781#M88326</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-23T06:25:44Z</dc:date>
    </item>
  </channel>
</rss>

