<?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/539161#M148500</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I fully agree with all the others that the LAG function is not ideal for this task (to say the least). But you could use this (inefficient) code in order to apply it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set kk end=last;
if _n_=1 then call execute('data want; set kk;');
call execute('new_marks=sum(marks,lag(new_marks));');
if last then call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: Or this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set kk;
do _n_=0, 1;
  if _n_ then output;
  new_marks=sum(marks,lag(new_marks));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Feb 2019 23:31:10 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2019-02-27T23:31:10Z</dc:date>
    <item>
      <title>LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539026#M148446</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anybody help me to create the code which make the cumulative sum for the variable Marks used in below data set with LAG Keyword/Function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input:&lt;/P&gt;&lt;P&gt;data kk;&lt;BR /&gt;input id names$ marks;&lt;BR /&gt;cards;&lt;BR /&gt;1 jai 10&lt;BR /&gt;2 sai 20&lt;BR /&gt;3 ram 30&lt;BR /&gt;4 shyam 40&lt;BR /&gt;5 hari 50&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;output&amp;nbsp; Should be&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;id names marks New_marks&lt;P&gt;1&amp;nbsp; &amp;nbsp; jai&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; sai&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;BR /&gt;3&amp;nbsp; &amp;nbsp; ram&amp;nbsp; &amp;nbsp; &amp;nbsp;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 60&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;shyam 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&lt;BR /&gt;5&amp;nbsp; &amp;nbsp;hari&amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 150&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please provide the output only with using LAG function not Looking with&amp;nbsp; Retain Keyword.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jaiganesh&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 27 Feb 2019 15:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539026#M148446</guid>
      <dc:creator>jaiganesh</dc:creator>
      <dc:date>2019-02-27T15:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539027#M148447</link>
      <description>And why don't you want to use retain? Have you read the examples in the online docs? This is such an easy task, it should be solvable with some research.</description>
      <pubDate>Wed, 27 Feb 2019 15:45:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539027#M148447</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-02-27T15:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539045#M148452</link>
      <description>I've been using Retain Keyword, one of my colleague has raise the concern of LAG function functionality and it's application in programming, Let me know if you need any other information.</description>
      <pubDate>Wed, 27 Feb 2019 16:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539045#M148452</guid>
      <dc:creator>jaiganesh</dc:creator>
      <dc:date>2019-02-27T16:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539050#M148457</link>
      <description>&lt;P&gt;Maxim 14: Use the Right Tool, in this case retain.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 16:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539050#M148457</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-27T16:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539079#M148465</link>
      <description>&lt;P&gt;If you show an attempt, happy to help you work through it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The documentation has some examples that are pretty helpful if you work through them first:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en#p0upu4wnn0xtwvn1p1bqaano69do" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en#p0upu4wnn0xtwvn1p1bqaano69do&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anybody help me to create the code which make the cumulative sum for the variable Marks used in below data set with LAG Keyword/Function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Input:&lt;/P&gt;
&lt;P&gt;data kk;&lt;BR /&gt;input id names$ marks;&lt;BR /&gt;cards;&lt;BR /&gt;1 jai 10&lt;BR /&gt;2 sai 20&lt;BR /&gt;3 ram 30&lt;BR /&gt;4 shyam 40&lt;BR /&gt;5 hari 50&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="dgrid-content ui-widget-content"&gt;
&lt;DIV class=" dgrid-row dgrid-row-odd ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;output&amp;nbsp; Should be&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class=" dgrid-row ui-state-default dgrid-row-even dgrid-selected ui-state-active"&gt;id names marks New_marks
&lt;P&gt;1&amp;nbsp; &amp;nbsp; jai&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; sai&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;BR /&gt;3&amp;nbsp; &amp;nbsp; ram&amp;nbsp; &amp;nbsp; &amp;nbsp;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 60&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;shyam 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 100&lt;BR /&gt;5&amp;nbsp; &amp;nbsp;hari&amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 150&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide the output only with using LAG function not Looking with&amp;nbsp; Retain Keyword.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Jaiganesh&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 18:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539079#M148465</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-27T18:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539106#M148475</link>
      <description>Mentioned Link does not help me much with respect to certain with LAG Function cumulative addition.&lt;BR /&gt;&lt;BR /&gt;Can you provide anything else</description>
      <pubDate>Wed, 27 Feb 2019 20:01:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539106#M148475</guid>
      <dc:creator>jaiganesh</dc:creator>
      <dc:date>2019-02-27T20:01:06Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539107#M148476</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Mentioned Link does not help me much with respect to certain with LAG Function cumulative addition.&lt;BR /&gt;&lt;BR /&gt;Can you provide anything else&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because the lag() function is NOT the right tool for your task, period.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 20:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539107#M148476</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-27T20:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539111#M148478</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Mentioned Link does not help me much with respect to certain with LAG Function cumulative addition.&lt;BR /&gt;&lt;BR /&gt;Can you provide anything else&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Many people are saying that LAG is not the right tool for this purpose. What more do you need? Furthermore, there is no documentation that we can point to that says function X should not be used to perform Task A. People don't write documentation listing all the things that a specific function can't do.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 20:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539111#M148478</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-02-27T20:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539121#M148483</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Lag function returns the value from a previous input record. Lag1 (or just Lag) returns the value from the record preceding the current record, Lag2 from the one before that and so on. There is no limit on the number of Lags other than the available memory, so if you have 1000 records, you can use Lag1 - Lag999.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you cannot use Lag on variables created in the data step, so you cannot accumulate values using Lag. Here is an example using Lag on your data to give the wanted result, so you can see what happens:&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;data kk;
input id names$ marks;
cards;
1 jai 10
2 sai 20
3 ram 30
4 shyam 40
5 hari 50
;
run;

data want; set kk; 
	lag1marks = lag1(marks);
	lag2marks = lag2(marks);
	lag3marks = lag3(marks);
	lag4marks = lag4(marks);
	New_marks = sum(marks,lag1marks,lag2marks,lag3marks,lag4marks);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="lag.gif" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27552iD7B79AC6FAEA5D9F/image-size/large?v=v2&amp;amp;px=999" role="button" title="lag.gif" alt="lag.gif" /&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;This works with your 5 observations, because the code uses Lag1 - Lag4, but it cannot be made dynamic, at least not without a lot of macro coding. If there were more observations, it would give a rolling sum over the last 5 observarions, which might be useful in some cases, but to use Lag to make a total summation over a column in a data set is - politely expressed - not advisable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 20:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539121#M148483</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-02-27T20:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539137#M148489</link>
      <description>&lt;P&gt;Thanks a lot for you time and effort.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Jaiganesh&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 20:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539137#M148489</guid>
      <dc:creator>jaiganesh</dc:creator>
      <dc:date>2019-02-27T20:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539139#M148491</link>
      <description>&lt;P&gt;The function LAG is no appropriate here.&lt;/P&gt;
&lt;P&gt;Ask your colleague to enlighten us and show us how it's done.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note this was already asked by someone else and on the same day (and with equivalent replies).&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/New-SAS-User/How-to-calculate-cumulative-sum-using-Lag-function/td-p/538908" target="_blank"&gt;https://communities.sas.com/t5/New-SAS-User/How-to-calculate-cumulative-sum-using-Lag-function/td-p/538908&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Colleague or classmate?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 21:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539139#M148491</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-02-27T21:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539161#M148500</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I fully agree with all the others that the LAG function is not ideal for this task (to say the least). But you could use this (inefficient) code in order to apply it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set kk end=last;
if _n_=1 then call execute('data want; set kk;');
call execute('new_marks=sum(marks,lag(new_marks));');
if last then call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit: Or this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set kk;
do _n_=0, 1;
  if _n_ then output;
  new_marks=sum(marks,lag(new_marks));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Feb 2019 23:31:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539161#M148500</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-02-27T23:31:10Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539602#M148710</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm really puzzled.&lt;/P&gt;
&lt;P&gt;What is the functional difference between&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; kk&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
do _n_&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; _n_ &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; output&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  new_marks&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sum&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;marks&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;new_marks&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
set kk;
new_marks=sum(marks,lag(new_marks));
output;
new_marks=sum(marks,lag(new_marks));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;(the first works, the second not, although the summarization and output statements execute in the same order)&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 12:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539602#M148710</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-01T12:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539616#M148717</link>
      <description>&lt;P&gt;The crucial point is that each occurrence of the LAG function in the DATA step corresponds to a separate queue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, in the first code &lt;EM&gt;one&lt;/EM&gt; queue operates twice: The idea is that the queue should always output the previous cumulative sum (to be added to the current value of MARKS in order to obtain the desired value of NEW_MARKS). This happens in the first iteration of the DO loop. The second iteration of the loop is required because the calculated value of NEW_MARKS must be fed into the queue, so that it will be output in the next call of the LAG function (in the next iteration of the DATA step). This cannot be accomplished during the calculation because obviously the value just being calculated is not available yet and hence a &lt;EM&gt;missing&lt;/EM&gt; value is fed into the queue (since NEW_MARKS is not RETAINed). This missing value is then returned in the second iteration of the DO loop, leading (temporarily) to a "wrong" value of NEW_MARKS, namely NEW_MARKS=MARKS, but this doesn't matter because it happens after the OUTPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second code creates two separate queues. The first one does always the same in each iteration of the DATA step:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It &lt;EM&gt;outputs&lt;/EM&gt; a missing value (in the first iteration of the DATA step this is the usual behavior in the first call of any LAG function, in subsequent iterations it's because of item 2 below).&lt;/LI&gt;
&lt;LI&gt;It &lt;EM&gt;stores&lt;/EM&gt; a missing value because the unretained variable NEW_MARKS has been initialized to missing and not yet assigned a value at the time when the corresponding LAG function is called.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Hence, the value of NEW_MARKS which is output is always SUM(MARKS, .) = MARKS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second queue stores the value of NEW_MARKS created in the previous assignment statement (i.e. [with the data in dataset &lt;FONT face="courier new,courier"&gt;kk&lt;/FONT&gt;] 10, 20, 30, ... -- the values of MARKS, see above) and outputs the previous one, thus assigning temporary values 10, 30, 50, 70, ... to NEW_MARKS, but these aren't output to dataset WANT2 anyway, nor are they retained. So, the second assignment statement is useless.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 13:53:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539616#M148717</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-03-01T13:53:03Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539656#M148735</link>
      <description>&lt;P&gt;&lt;EM&gt;That's&lt;/EM&gt; the explanation. Two queues vs. one queue.&lt;/P&gt;
&lt;P&gt;Thank you very much.&lt;/P&gt;
&lt;P&gt;Maxim 13 in action.&lt;/P&gt;</description>
      <pubDate>Fri, 01 Mar 2019 15:01:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539656#M148735</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-03-01T15:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539732#M148763</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;&lt;/P&gt;
&lt;P&gt;That was really interesting, thank you so much! - I am sorry to have mislead&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240933"&gt;@jaiganesh&lt;/a&gt;, but I never realized that lag is not coupled to the input buffer, but works on any assignment, so a construction like this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;&lt;BR /&gt;    do a = 1 to 5;&lt;BR /&gt;        b = lag(a);&lt;BR /&gt;        c = lag(b);&lt;BR /&gt;        d = lag(c);&lt;BR /&gt;        e = lag(d);&lt;BR /&gt;        put a= b= c= d= e=;&lt;BR /&gt;    end;&lt;BR /&gt;run;

a=1 b=. c=. d=. e=.
a=2 b=1 c=. d=. e=.
a=3 b=2 c=1 d=. e=.
a=4 b=3 c=2 d=1 e=.
a=5 b=4 c=3 d=2 e=1
&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 Mar 2019 18:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function/m-p/539732#M148763</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-03-01T18:03:28Z</dc:date>
    </item>
  </channel>
</rss>

