<?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: When to use If or If-Then in Data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647985#M193988</link>
    <description>Queue vs stack. Noted. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 15 May 2020 10:04:26 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2020-05-15T10:04:26Z</dc:date>
    <item>
      <title>When to use If or If-Then in Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647959#M193973</link>
      <description>&lt;P&gt;I am wondering why these two statements give different results. The difference is where I set the If condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dt;&lt;BR /&gt;set dt;&lt;BR /&gt;lyear = lag(year);&lt;BR /&gt;lfirm = lag(firm);&lt;BR /&gt;if lfirm=firm;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data dt;&lt;BR /&gt;set dt;&lt;BR /&gt;lyear = lag(year);&lt;BR /&gt;if lfirm=firm then lfirm = lag(firm);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 02:48:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647959#M193973</guid>
      <dc:creator>bruinsteph</dc:creator>
      <dc:date>2020-05-15T02:48:55Z</dc:date>
    </item>
    <item>
      <title>Re: When to use If or If-Then in Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647962#M193974</link>
      <description>&lt;P&gt;Never use the LAG function inside a condition - you will get unpredictable results.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 03:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647962#M193974</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-05-15T03:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: When to use If or If-Then in Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647963#M193975</link>
      <description>&lt;P&gt;The lag function manages its stack when it's called.&lt;/P&gt;
&lt;P&gt;If you don't call it for every observation (for example you only call it when a condition is true), you don't get every value.&lt;/P&gt;
&lt;P&gt;This is might a desirable outcome, but most often it is not.&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 04:02:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647963#M193975</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-15T04:02:22Z</dc:date>
    </item>
    <item>
      <title>Re: When to use If or If-Then in Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647983#M193987</link>
      <description>&lt;P&gt;The IF statement without THEN (IF &amp;lt;condition&amp;gt;) is equivalent to IF NOT &amp;lt;condition&amp;gt; THEN DELETE;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, your first data step could also be written as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dt;
  set dt;
  lyear = lag(year);
  lfirm = lag(firm);
  if lfirm ne firm then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The second data step is (as others have also remarked) using a very dangerous approach: calling a LAG function conditionally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's what the LAG function does:&lt;/P&gt;
&lt;P&gt;It has an internal queue. Not a stack as it was called by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;, values in a queue are FIFO (first in, first out), while values in a stack are LIFO (last in, first out). This makes no difference when using the basic LAG function (the queue has a length of one value only), but when using e.g. LAG2 or LAG3, the queue is longer. Sorry for being a bit pedantic...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stuff only gets pushed into the queue when the LAG function is called.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what happens in your second data step? The statement&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if lfirm=firm then lfirm = lag(firm);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;does not do anything meaningful in this context. LFIRM has not been set to a value, so it is missing to begin with. The LAG function will only be called when FIRM is also missing, meaning that all the LAG function does is pull the old missing value from the queue, and push a new missing value (the current value of FIRM) into the queue. This means that LFIRM will always be missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 May 2020 09:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647983#M193987</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-05-15T09:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: When to use If or If-Then in Data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647985#M193988</link>
      <description>Queue vs stack. Noted. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 15 May 2020 10:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-to-use-If-or-If-Then-in-Data-step/m-p/647985#M193988</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-05-15T10:04:26Z</dc:date>
    </item>
  </channel>
</rss>

