<?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 Condition of calculating Lag in a datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715520#M221015</link>
    <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding LAG function, I have two questions:&lt;/P&gt;
&lt;P&gt;1. I saw an example online and see people set the condition&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.id then lag_value = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the picture below&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1612001716136.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54147i843842F45338250B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1612001716136.png" alt="My97_0-1612001716136.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I do not know what is the code mentioned above for and without this code, whether SAS will generate the missing for the first observation. I ask this question because the official guide from SAS did not mention setting this condition&lt;/P&gt;
&lt;P&gt;Link: The one I retrieve the example above:&amp;nbsp;&lt;A href="https://www.listendata.com/2016/08/4-ways-to-calculate-lag-and-lead-in-sas.html#:~:text=In%20SAS%2C%20the%20LAG%20function,measuring%20lag%20of%20third%20order.&amp;amp;text=Suppose%20you%20are%20asked%20to,order%20by%20a%20grouping%20variable" target="_blank"&gt;https://www.listendata.com/2016/08/4-ways-to-calculate-lag-and-lead-in-sas.html#:~:text=In%20SAS%2C%20the%20LAG%20function,measuring%20lag%20of%20third%20order.&amp;amp;text=Suppose%20you%20are%20asked%20to,order%20by%20a%20grouping%20variable&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The official guide for SAS:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Can we use the LAG function directly in a calculation without defining it previously. What I mean is, can I do something like the code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
   y= lag(x) + lag2(x) + z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or I must do something as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

  set have;

   lagx=lag(x);
   lag2x=lag2(x);
   y= lagx+lag2x+z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 30 Jan 2021 10:21:48 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-01-30T10:21:48Z</dc:date>
    <item>
      <title>Condition of calculating Lag in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715520#M221015</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding LAG function, I have two questions:&lt;/P&gt;
&lt;P&gt;1. I saw an example online and see people set the condition&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.id then lag_value = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the picture below&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1612001716136.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54147i843842F45338250B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1612001716136.png" alt="My97_0-1612001716136.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I do not know what is the code mentioned above for and without this code, whether SAS will generate the missing for the first observation. I ask this question because the official guide from SAS did not mention setting this condition&lt;/P&gt;
&lt;P&gt;Link: The one I retrieve the example above:&amp;nbsp;&lt;A href="https://www.listendata.com/2016/08/4-ways-to-calculate-lag-and-lead-in-sas.html#:~:text=In%20SAS%2C%20the%20LAG%20function,measuring%20lag%20of%20third%20order.&amp;amp;text=Suppose%20you%20are%20asked%20to,order%20by%20a%20grouping%20variable" target="_blank"&gt;https://www.listendata.com/2016/08/4-ways-to-calculate-lag-and-lead-in-sas.html#:~:text=In%20SAS%2C%20the%20LAG%20function,measuring%20lag%20of%20third%20order.&amp;amp;text=Suppose%20you%20are%20asked%20to,order%20by%20a%20grouping%20variable&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The official guide for SAS:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Can we use the LAG function directly in a calculation without defining it previously. What I mean is, can I do something like the code below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
   y= lag(x) + lag2(x) + z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or I must do something as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

  set have;

   lagx=lag(x);
   lag2x=lag2(x);
   y= lagx+lag2x+z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 10:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715520#M221015</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-30T10:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Condition of calculating Lag in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715521#M221016</link>
      <description>&lt;P&gt;1) This is done to prevent the lag function from 'crossing' by-groups. This is only an issue if you deal with data with different by-groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) Yes, no problem. However be aware that lag2(x) returns a missing value for the first two observations. Therefore, you will get a missing value for y in the first two obs because you use the + operator and not the Sum Function. See the small example below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input x z;
datalines;
1 2
3 4
5 6
7 8
9 0
;

data want;
   set have;
   y = lag(x) + lag2(x) + z;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 30 Jan 2021 10:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715521#M221016</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-30T10:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Condition of calculating Lag in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715522#M221017</link>
      <description>&lt;P&gt;Usually, when programmers have questions about the Lag Function, it boils down to a common misunderstanding about the Lag Function. The Lag Function is not a 'lookback' function. Rather it is a 'queue' function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;When you use the Lag2 Function, SAS sets up a queue with two elements. Let us think of it like this [ . | . ]. Both elements are missing at the start of execution. Each time it executes, SAS returns the right-most value from the queue. Furthermore, the present value of the value we want to lag, is inserted into the queue from the left. The documentation describes a queue from bottom to top. However, I find it easier to present here with left to right. The principle is the same.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I've written a small blog post about the Lag Function that should clear up most misunderstandings &lt;A href="https://sasnrd.com/sas-lag-function-by-group-example/" target="_self"&gt;here&lt;/A&gt; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 10:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715522#M221017</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-30T10:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Condition of calculating Lag in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715524#M221018</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your clear answer!&lt;/P&gt;
&lt;P&gt;In case I want to have a result as this picture&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="My97_0-1612003348787.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54148i6B078F7C35DDAEC9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="My97_0-1612003348787.png" alt="My97_0-1612003348787.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I must follow strictly the code they posted, isn't it? Because my data is also like that.&lt;/P&gt;
&lt;P&gt;And I think it is what you mentioned "crossing" by-groups.&lt;/P&gt;
&lt;P&gt;So, if I want to perform my calculation, I must do two different datasteps&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by id;
 lagx = lag(x);&lt;BR /&gt; lag2x=lag2(x);
 if first.id then lagx = . and lag2x=.;&lt;BR /&gt; if second.id then lag2x=.;
run;

data want1;
 set want;
  y=lagx + lag2x +z;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is it correct? or is there any way to shorten it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 10:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715524#M221018</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-30T10:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Condition of calculating Lag in a datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715527#M221019</link>
      <description>&lt;P&gt;Well actually, in this case you have to be extra careful not crossing by-groups because you have the lag1 and the lag2 function to deal with. So for the Lag1 Function (same as Lag), you do not want the lagged value in the first obs of each by group and for the Lag2 Function you do not want the lagged value in the first 2 observations of the by-group. Makes sense?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you may not want to have missing values for y is just lag2x is missing. Therefore, you can use the Sum Function instead of +.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: Do you have more lags than this in your actual data? If so, there are better ways to do this &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id x z;
datalines;
1 25 1 
1 30 2 
1 35 3 
1 40 4 
2 25 5 
2 30 6 
2 37 7 
;

data want;
   set have;
   by id;
   if first.id then c = 0; c + 1;
   lagx  = ifn(c = 1      , ., lag(x));
   lag2x = ifn(c in (1, 2), ., lag2(x));
   *y = lagx + lag2x + z;
   y = sum(lagx, lag2x, z);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jan 2021 10:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715527#M221019</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-30T10:58:34Z</dc:date>
    </item>
  </channel>
</rss>

