<?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: About using Lag Function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624832#M184102</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293869"&gt;@maozsoy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;Regarding the 7th row, I think you're right because the 6th row corresponds to a "first. " so the statement is not true.&lt;/P&gt;</description>
    <pubDate>Fri, 14 Feb 2020 13:46:25 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-02-14T13:46:25Z</dc:date>
    <item>
      <title>About using Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624761#M184080</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am using SAS EG 7.1.&amp;nbsp;&lt;/SPAN&gt;I have data that named 'work.have' and it contains 'var' feature for three id. I want to calculate difference of vars for every id. To do this, i used lag() function on 3 different types but its gave different results. (the right of its; "DIFF_1")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Actually i expect same result from all of types. How happened different result, can you explain this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
input subs_id subs_var;
cards;
111111  70
111111  50
111111  100
222222  80
222222  90
333333  60
333333  90
333333  130
333333  80
;
run;

proc sort data = work.have; by subs_id; run;

data work.want;
set  work.have;

/*	way - 1	*/
lag_id = lag(subs_id);
lag_var = lag(subs_var);
if lag_id = subs_id then DIFF_1 = lag_var - subs_var; else DIFF_1 = .;

/*	way - 2	*/
if lag(subs_id) = subs_id then DIFF_2 = lag(subs_var) - subs_var; else DIFF_2 = .;

/*	way - 3	*/
by subs_id;
if not first.subs_id then DIFF_3 = lag(subs_var) - subs_var; else DIFF_3 = .;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lag.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36114i1108DB8900A357DB/image-size/large?v=v2&amp;amp;px=999" role="button" title="lag.PNG" alt="lag.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 08:52:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624761#M184080</guid>
      <dc:creator>maozsoy</dc:creator>
      <dc:date>2020-02-14T08:52:19Z</dc:date>
    </item>
    <item>
      <title>Re: About using Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624769#M184082</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293869"&gt;@maozsoy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an explanation from the documentation about the &lt;A href="https://documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en#n0dhbp93jiayvpn142mxik79uxi6" target="_self"&gt;LAG ()&lt;/A&gt; function:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN class="xisDoc-noteGenText"&gt;Note: &lt;/SPAN&gt;&lt;SPAN&gt;Storing values at the bottom of the queue and returning values from the top of the queue occurs only when the function is executed. &lt;U&gt;An occurrence of the LAG&lt;/U&gt;&lt;/SPAN&gt;&lt;U&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;n&lt;/EM&gt;&lt;/U&gt;&lt;SPAN&gt;&lt;U&gt; function that is executed conditionally stores and return values only from the observations for which the condition is satisfied&lt;/U&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;DIV class="page" title="Page 354"&gt;
&lt;DIV class="layoutArea"&gt;
&lt;DIV class="column"&gt;
&lt;P&gt;&lt;SPAN&gt;So, the best practice is to create a lagged value in an assignment statement before using it in a conditional statement. Otherwise, it can lead to unexpected results.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-&amp;gt; So WAY 1 would be the proper approach in your case:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
	set work.have;

	/*	way - 1	*/
	lag_id=lag(subs_id);
	lag_var=lag(subs_var);

	if lag_id=subs_id then DIFF_1=lag_var - subs_var;
	else DIFF_1=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 14 Feb 2020 09:46:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624769#M184082</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-14T09:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: About using Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624795#M184091</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;thank you for your reply. OK i will prefer way that your mentioned but i didn't understand working logic.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example 7th row, "if statement" is true (subs_id = 333333 and 6th row upper subs_id = 333333) but lag function doesn't work right and i guess it stores 5th instead of 6th row&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 12:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624795#M184091</guid>
      <dc:creator>maozsoy</dc:creator>
      <dc:date>2020-02-14T12:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: About using Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624832#M184102</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293869"&gt;@maozsoy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're welcome.&lt;/P&gt;
&lt;P&gt;Regarding the 7th row, I think you're right because the 6th row corresponds to a "first. " so the statement is not true.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 13:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624832#M184102</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-14T13:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: About using Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624838#M184106</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293869"&gt;@maozsoy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;thank you for your reply. OK i will prefer way that your mentioned but i didn't understand working logic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example 7th row, "if statement" is true (subs_id = 333333 and 6th row upper subs_id = 333333) but lag function doesn't work right and i guess it stores 5th instead of 6th row&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Because it &lt;STRONG&gt;retrieved&lt;/STRONG&gt; the value stored on the 5th observation.&amp;nbsp; The LAG() function call on the 7th observation returns the value from the 5th observation because that last time the LAG() function ran was on the 5th observation. On the 6th observation you never ran the LAG() function so there was no way for the value to get stored.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 14:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/About-using-Lag-Function/m-p/624838#M184106</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-14T14:02:24Z</dc:date>
    </item>
  </channel>
</rss>

