<?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: If statement Short circuiting and  Lag function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473545#M121544</link>
    <description>&lt;P&gt;I can't explain it.&amp;nbsp; It's difficult to even come up with a valid test program under these conditions.&amp;nbsp; The best I could do was to demonstrate that this condition is true:&amp;nbsp; a ^= lag(a)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do that, I substituted for this statement:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; or &lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;These are the statements that I replaced it with:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token punctuation"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; (a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)) * 0&amp;nbsp;or &lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;. &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a) or (&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;.) * 0&amp;nbsp;&lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jun 2018 20:53:43 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-06-26T20:53:43Z</dc:date>
    <item>
      <title>If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473489#M121517</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to understand the Output from the below piece of SAS Code .&amp;nbsp; This Code is actually a simplified version to the one posted by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp; in his blog&amp;nbsp; back in 2012 :&lt;A href="https://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/" target="_blank"&gt;https://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the Code :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile datalines dlm=',' dsd;
  input a b;
  datalines;
4272451,17878
4272451,17878
run;
 
data testLags;
  retain e 1;
  set test;
  if a=lag(a) and b&amp;gt;lag(b) then
    e=e+1;
  else if a^=lag(a) or lag(a)=. then
    e=1;
  run;
 
proc print data=testLags;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above gives the following output :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21420i222CEE18538656FE/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;My&amp;nbsp; question is&amp;nbsp; why the value of variable e =1&amp;nbsp; instead of&amp;nbsp; 2 in the&amp;nbsp;second observation?&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;When IF statements combine two conditions with an AND,&amp;nbsp;if the first condition resolves to false, the second condition is not evaluated.&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;P&gt;This is known as "Short circuiting" of the Boolean expression as i understand it .&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So basically during the first iteration of the above Data step , the second condition&amp;nbsp; : b &amp;gt; lag(b)&amp;nbsp; in the IF&amp;nbsp;Statement is never processed since first condition in the IF statement&amp;nbsp; ie&amp;nbsp; a&amp;nbsp;= lag(a)&amp;nbsp; &amp;nbsp;resolves to FALSE. This would mean in the &lt;STRONG&gt;second iteration&lt;/STRONG&gt; the IF statement&amp;nbsp; should&amp;nbsp;&amp;nbsp;evaluate as below :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;&amp;nbsp;4272451 =&amp;nbsp;4272451&amp;nbsp; AND&amp;nbsp; 17878 &amp;gt;&amp;nbsp; .&amp;nbsp; &amp;nbsp;,&amp;nbsp; which should evaluate to TRUE and so the variable e should&amp;nbsp;be incremented by 1 and become 2 in the second observation.&amp;nbsp; Note the lag(b)&amp;nbsp; in the second&amp;nbsp; iteration&amp;nbsp; should resolve to a missing value&amp;nbsp; represented by a&amp;nbsp; .&amp;nbsp;(&amp;nbsp;period ) because this is the first time that lag(b) is getting processed in the Data Step .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what&amp;nbsp; is going on here ?&amp;nbsp;Am&amp;nbsp;i missing something ?&amp;nbsp; Why isn't&amp;nbsp; e=2&amp;nbsp; in the second observation?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 17:32:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473489#M121517</guid>
      <dc:creator>pchegoor</dc:creator>
      <dc:date>2018-06-26T17:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473492#M121519</link>
      <description>17878 is not greater than 17878. In the second row of your datalines, try setting b to 17879. Then you will get the results you want. Otherwise, the code is right in this case and it shouldn't be incrementing.</description>
      <pubDate>Tue, 26 Jun 2018 17:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473492#M121519</guid>
      <dc:creator>lrudolphi</dc:creator>
      <dc:date>2018-06-26T17:40:42Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473494#M121520</link>
      <description>&lt;P&gt;Great use case for the DATA step debugger in SAS Enterprise Guide.&amp;nbsp; In this case, the second OBS does not trigger either condition (the IF or the ELSE IF), so the value of&amp;nbsp;&lt;STRONG&gt;e&lt;/STRONG&gt; remains at 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;div class="lia-vid-container video-embed-center"&gt;&lt;div id="lia-vid-phNHRtZjE6XDIm4mKGTh42MFc0eMPR-yw704h512r727" class="lia-video-brightcove-player-container"&gt;&lt;video-js data-video-id="6064693264001" data-account="6058004174001" data-player="default" data-embed="default" class="vjs-fluid" controls="" data-application-id="" style="width: 100%; height: 100%;"&gt;&lt;/video-js&gt;&lt;/div&gt;&lt;script src="https://players.brightcove.net/6058004174001/default_default/index.min.js"&gt;&lt;/script&gt;&lt;script&gt;(function() {  var wrapper = document.getElementById('lia-vid-phNHRtZjE6XDIm4mKGTh42MFc0eMPR-yw704h512r727');  var videoEl = wrapper ? wrapper.querySelector('video-js') : null;  if (videoEl) {     if (window.videojs) {       window.videojs(videoEl).ready(function() {         this.on('loadedmetadata', function() {           this.el().querySelectorAll('.vjs-load-progress div[data-start]').forEach(function(bar) {             bar.setAttribute('role', 'presentation');             bar.setAttribute('aria-hidden', 'true');           });         });       });     }  }})();&lt;/script&gt;&lt;a class="video-embed-link" href="https://communities.sas.com/t5/video/gallerypage/video-id/phNHRtZjE6XDIm4mKGTh42MFc0eMPR-y"&gt;(view in My Videos)&lt;/a&gt;&lt;/div&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 17:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473494#M121520</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2018-06-26T17:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473511#M121524</link>
      <description>&lt;P&gt;I can't prove it, but based on your results the process must be this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first statement, SAS evaluates both LAG functions first, then makes the comparisons as the second step.&amp;nbsp; So LAG(B) does actually execute on the first statement.&amp;nbsp; The comparison comparing b to lag(b) doesn't execute, but the lag function itself has already executed.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 18:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473511#M121524</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-26T18:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473524#M121530</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp; and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp; &amp;nbsp;. Assuming your explanation is correct , how do i make sense of the&amp;nbsp; discrepancy seen in the &lt;STRONG&gt;5th observation&lt;/STRONG&gt;&amp;nbsp; from Chris's&amp;nbsp; blog :&amp;nbsp;&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/" target="_blank"&gt;https://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should'nt the value of variable&amp;nbsp; f&amp;nbsp; be 2&amp;nbsp; &amp;nbsp;instead of 1&amp;nbsp; ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile datalines dlm=',' dsd;
  input a b c;
  datalines;
4272451,17878,17878 
4272451,17878,17878 
4272451,17887,17887 
4272454,17878,17878 
4272454,17881,17881 
4272454,17893,17893 
4272455,17878,17878 
4272455,17878,18200 
run;
 
data testLags;
  retain e f ( 1 1);
  set test;
  if a=lag(a) and b&amp;gt;lag(b) then
    e=e+1;
  else if a^=lag(a) or lag(a)=. then
      e=1;
  if a^=lag(a) or lag(a)=. then
      f=1;
  else if a=lag(a) and b&amp;gt;lag(b) then
      f=f+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs    e    f       a         b        c

 1     1    1    4272451    17878    17878
 2     1    1    4272451    17878    17878
 3     2    2    4272451    17887    17887
 4     1    1    4272454    17878    17878
 5     2    &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/FONT&gt;    4272454    17881    17881
 6     3    2    4272454    17893    17893
 7     1    1    4272455    17878    17878
 8     1    1    4272455    17878    18200&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 19:29:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473524#M121530</guid>
      <dc:creator>pchegoor</dc:creator>
      <dc:date>2018-06-26T19:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473528#M121533</link>
      <description>&lt;P&gt;That's what i thought initially . But if you look at the blog or my reply below , i cannot understand how to make sense of the value of variable f in the obervation 5 . why is it 1 instead 2 ?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 19:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473528#M121533</guid>
      <dc:creator>pchegoor</dc:creator>
      <dc:date>2018-06-26T19:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473545#M121544</link>
      <description>&lt;P&gt;I can't explain it.&amp;nbsp; It's difficult to even come up with a valid test program under these conditions.&amp;nbsp; The best I could do was to demonstrate that this condition is true:&amp;nbsp; a ^= lag(a)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do that, I substituted for this statement:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; or &lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;These are the statements that I replaced it with:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token punctuation"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; (a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)) * 0&amp;nbsp;or &lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;. &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;&amp;nbsp; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; a&lt;SPAN class="token operator"&gt;^=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a) or (&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;(a)&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;.) * 0&amp;nbsp;&lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 20:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473545#M121544</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-26T20:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473552#M121545</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/14002"&gt;@pchegoor&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;I just walked&amp;nbsp;through this data step manually and applied my understanding of the LAG function (essentially based on practical experience) -- and was happy to see that the result&amp;nbsp;matches the actual output dataset. According to my notes, the IF condition which would need to be met in order to trigger the incrementation of F in the 5th observation reads "&lt;FONT face="courier new,courier"&gt;if 54=51 and 81&amp;gt;87&lt;/FONT&gt;" -- i.e., not even one of the two expressions is true, hence no incrementation occurs and F remains 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think, key points &lt;SPAN&gt;are:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;Each occurrence of the LAG function in a data step has its separate queue.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;ELSE clauses (and in particular LAG functions contained therein) are not executed if the preceding IF condition was met.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;I did &lt;EM&gt;not&lt;/EM&gt; assume any short-circuiting within a Boolean expression (although I know that it occurs under certain other circumstances).&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;(I may be wrong, this is just based on my experience 1997 - 2018.)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Perhaps I can think about and elaborate on this a little more tomorrow (it's close to midnight in my time zone).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jun 2018 21:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473552#M121545</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-26T21:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473693#M121585</link>
      <description>&lt;P&gt;Hi again,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I have put my notes in tabular form, as shown below.&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="LAGtest.png" style="width: 583px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21434i9F8579050596495A/image-size/large?v=v2&amp;amp;px=999" role="button" title="LAGtest.png" alt="LAGtest.png" /&gt;&lt;/span&gt;&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;
&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;To save horizontal space, I've transposed the table. So, observations are aligned in columns and the input data are entered in rows "a" and "b". Please note that I have shed variable C as well as all but the last two digits of A and B in order to reduce redundancy and clutter. (This is where the "54", "51" etc. in my previous post came from.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Based on my assumption that each occurrence of the LAG function in a data step has its own queue, there are eight rows containing LAG function values (lag(a), lag(b)), each of which is followed by a row for the (invisible) value contained in the associated queue (q1, q2, etc.). They occur in the same order as in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "lag(a)" or "lag(b)" terms in the four Boolean expressions correspond to the rows of the same headers and with the same background color. "N/A" is entered if an ELSE clause is not executed for logical reasons. Obviously, rows "e" and "f" contain the results, followed by verbal descriptions of what (I think) happened.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a screenshot of an Excel table. Virtually all cells were computed using Excel formulas (which in turn implement my assumptions about the workings of this data step). So, I can enter new data in rows "a" and "b" and let Excel calculate e and f. (I tested this with a few random permutations of the data.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If someone is interested, I can upload the xlsx file, whose formulas use &lt;EM&gt;German&lt;/EM&gt; function names, though.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't read Chris' blog article in detail yet. In particular, I haven't checked if I was correct in disregarding short circuiting within Boolean expressions. However, at least for the current input data (and the few permutations I checked) the results match those obtained with SAS.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 12:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473693#M121585</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-27T12:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473702#M121588</link>
      <description>&lt;P&gt;Wow, thanks for the detailed treatment&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;!&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My blog does cite the practice of Boolean short-circuiting, which would allow the language processor to discontinue evaluating a Boolean expression as soon as one part of it evaluated to False (or 0).&amp;nbsp; However, I can't say for certain under which conditions SAS employs this practice.&amp;nbsp; It's well-defined in other compiled programming languages like C, and experienced programmers may be tempted to rely on it.&amp;nbsp; However, I think it's a good practice to be more explicit about the conditions in your SAS program for readability sake as well as for a more deterministic approach.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 12:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473702#M121588</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2018-06-27T12:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473746#M121605</link>
      <description>&lt;P&gt;Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It helps to see it all listed out here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The key part that I missed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the 5th observation, each instance of lag(b) has its own queue.&amp;nbsp; When computing F,&amp;nbsp;lag(b) is 87, not 78.&amp;nbsp; On the 4th observation, this particular lag(b) did not execute, so when it executes on the 5th observation it is picking up B from observation #3.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 14:18:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473746#M121605</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-27T14:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473749#M121606</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thank you for your detailed explanation of the behavior various Lag functions in this Data Step. This has been a good discussion.&amp;nbsp; I&amp;nbsp; initially thought this was some kind of a Bug with the Lag function behaving weirdly as in case of the 5th observation. But your detailed analysis of each lag function occurrence&amp;nbsp;having its own queue makes a lot of sense to explain this and demonstrate that is in fact&amp;nbsp;not a Bug but it is working as designed. As mentioned in Chris's Blog, the best way to avoid this kind of behavior is to declare 2 variables for lag(a)&amp;nbsp; and lag(b) respectively&amp;nbsp;before the IF statements and then use these variables in the conditions of the If statements. This ensures lag(a)&amp;nbsp; and lag(b) are each processed only once during each iteration of the Data Step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The&amp;nbsp; Data Step example can further be simplified as below to show the same kind of behavior, this time in only the second observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  infile datalines dlm=',' dsd;
  input a b;
  datalines;
4272451,17878
4272451,17879 
;
run;
 
data testLags;
  retain e f ( 1 1);
  set test;
  
       if a=lag(a) and b&amp;gt;lag(b) then e=e+1;     /**(1)**/
  else if a^=lag(a) or lag(a)=. then e=1;       /**(2)**/
      
      
       if a^= lag(a) or lag(a)=. then f=1;      /**(3)**/
  else if a=lag(a) and b&amp;gt;lag(b) then f=f+1;     /**(4)**/
      
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21445i0423B00B16995346/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks also to others who participated in this discussion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 14:28:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473749#M121606</guid>
      <dc:creator>pchegoor</dc:creator>
      <dc:date>2018-06-27T14:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473832#M121635</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I haven't read Chris' blog article in detail yet. In particular, I haven't checked if I was correct in disregarding short circuiting within Boolean expressions. However, at least for the current input data (and the few permutations I checked) the results match those obtained with SAS.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I've created a modified version of my Excel table, now implementing Boolean short-circuiting (for both "FALSE and X ==&amp;gt; FALSE" and "TRUE or X &lt;SPAN&gt;==&amp;gt;&lt;/SPAN&gt; TRUE"). Compared to the original table, 57 (!) cells have changed, including two values of E (obs. 5 and 6 have decreased by 1) and two values of F (&lt;SPAN&gt;obs.&amp;nbsp;3 and 6 have decreased by 1).&amp;nbsp;Boolean short-circuiting for "FALSE and X ==&amp;gt; FALSE" alone changes two values of E and one of F.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Consequently, Boolean short-circuiting &lt;EM&gt;does not&lt;/EM&gt; seem to occur in this specific SAS program. It is rather the fact that ELSE clauses are not executed if the preceding IF condition is true.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Meanwhile, the exact conditions under which Boolean&amp;nbsp;short-circuiting occurs remain mysterious (to me; see examples under the spoiler).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
a=0; b=0;
if b=1 &amp;amp; 1/a&amp;gt;0;
run; /* No note about "Division by zero ..." in the log, seemingly because b=1 is false. */

data test;
a=0;
if 0 &amp;amp; 1/a&amp;gt;0;
run; /* This time "Division by zero ..." message appears, although 0 is obviously false. */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 17:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473832#M121635</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-06-27T17:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473861#M121655</link>
      <description>&lt;P&gt;+! for creative use of the spoiler feature &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 18:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/473861#M121655</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2018-06-27T18:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: If statement Short circuiting and  Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/474212#M121801</link>
      <description>This random occurence of Boolean shortcuiting seems a bit concerning to me.</description>
      <pubDate>Thu, 28 Jun 2018 18:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-Short-circuiting-and-Lag-function/m-p/474212#M121801</guid>
      <dc:creator>pchegoor</dc:creator>
      <dc:date>2018-06-28T18:59:53Z</dc:date>
    </item>
  </channel>
</rss>

