<?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 help in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253390#M48211</link>
    <description>&lt;P&gt;Your code have nothing to do with the date variable.&lt;BR /&gt;You forgot the semicolon in the third if statement.&lt;/P&gt;</description>
    <pubDate>Tue, 01 Mar 2016 11:19:11 GMT</pubDate>
    <dc:creator>mohamed_zaki</dc:creator>
    <dc:date>2016-03-01T11:19:11Z</dc:date>
    <item>
      <title>Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253388#M48209</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table (simplified for this) which has 3 columns. An ID column, a date column and a score column. I'll call this table "Have"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp;01/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;01/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to add a column which identifies whether the scores have increased over time. I have tried a lag function like so;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;length flag $20.;&lt;BR /&gt;if ID= lag(ID) and score&amp;gt; lag(score) then flag = 'Increased';&lt;BR /&gt;else if ID= lag(ID) and score&amp;lt; lag(&lt;SPAN&gt;score&lt;/SPAN&gt;) then flag = 'Decreased';&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;else if &lt;/SPAN&gt;&lt;SPAN&gt;ID&lt;/SPAN&gt;&lt;SPAN&gt;= lag(&lt;/SPAN&gt;&lt;SPAN&gt;ID&lt;/SPAN&gt;&lt;SPAN&gt;) then flag = 'No Change'&lt;/SPAN&gt; &lt;BR /&gt;else flag = '';&lt;BR /&gt;run;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to work on all cases except when the dates are the same, So I end up with the following (note the entry at the bottom should have increased)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; score &amp;nbsp; &amp;nbsp; &amp;nbsp; flag&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp;01/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Increased&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;01/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; No change&lt;/P&gt;
&lt;P&gt;B &amp;nbsp; &amp;nbsp;02/01/2015 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there something I'm doing wrong? (first time using the lag function)&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253388#M48209</guid>
      <dc:creator>troopon</dc:creator>
      <dc:date>2016-03-01T11:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253390#M48211</link>
      <description>&lt;P&gt;Your code have nothing to do with the date variable.&lt;BR /&gt;You forgot the semicolon in the third if statement.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:19:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253390#M48211</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2016-03-01T11:19:11Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253394#M48213</link>
      <description>&lt;P&gt;The missing semicolon is a typo when copying across.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's why I'm confused though, my query has nothing to do with the date variable so why hasn't the flag been populated for the bottom row? When I look at my full table it is the same every time the date is the same. I have sorted the data using ID and date but that's because I want to see changes over time for each person.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:22:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253394#M48213</guid>
      <dc:creator>troopon</dc:creator>
      <dc:date>2016-03-01T11:22:05Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253395#M48214</link>
      <description>&lt;P&gt;You want to use BY ID; to process the data in groups. &amp;nbsp;You can do it with LAG but that is too hard. &amp;nbsp;Then you are just left with DIF functioin instead of LAG. &amp;nbsp;Set DIF to missing at first.id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data score;
   input ID:$1. date:mmddyy. score;
   format date mmddyy.;
   cards;
A    01/01/2015            20
A    02/01/2015            30
B    01/01/2015            20
B    02/01/2015            20 
B    02/01/2015            30
C    01/01/2015            20
C    02/01/2015            30
C    01/01/2015            20
C    02/01/2015            20 
C    02/01/2015            30
;;;;
   run;
data score2;
   set score;
   by id;
   delta = sign(dif(score));
   if first.id then delta=.;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2101i18258CE95A93F74D/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253395#M48214</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-03-01T11:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253396#M48215</link>
      <description>&lt;P&gt;You may also want to consider a slight modification to the code,&lt;/P&gt;
&lt;PRE&gt;data have;
  input ID $ date $ score;
datalines;
A 01/01/2015 20
A 02/01/2015 30
B 01/01/2015 20
B 02/01/2015 20 
B 02/01/2015 30
;
run;

data want;
  set have;
  length flag $20.;
  if id=lag(id) then do;
    select;
      when(score &amp;gt; lag(score)) flag="Increased";
      when(score &amp;lt; lag(score)) flag="Decreased";
      otherwise flag="No change";
    end;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;The above illustrates the select clause which is useful for multiple if statements and when wrapped in the if statement clearly shows it only happens when id matches previous instance.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:25:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253396#M48215</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-01T11:25:55Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253399#M48216</link>
      <description>&lt;P&gt;Thanks, I did not know the dif function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Still not sure why my original didn't work but this does so thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253399#M48216</guid>
      <dc:creator>troopon</dc:creator>
      <dc:date>2016-03-01T11:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253400#M48217</link>
      <description>&lt;P&gt;Curious question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the difference between&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if id=lag(id) then do;
    select;
      when(score &amp;gt; lag(score)) flag="Increased";
      when(score &amp;lt; lag(score)) flag="Decreased";
      otherwise flag="No change";&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if id=lag(id) then do;
      if(score &amp;gt; lag(score)) flag="Increased";
      else if(score &amp;lt; lag(score)) flag="Decreased";
      else flag="No change";&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253400#M48217</guid>
      <dc:creator>troopon</dc:creator>
      <dc:date>2016-03-01T11:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253406#M48219</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;your program doesn't work. &amp;nbsp;You are doing conditional lag which doesn't work here..&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2102i7471EE7E6AE5A10E/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:52:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253406#M48219</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-03-01T11:52:23Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253407#M48220</link>
      <description>&lt;P&gt;Well, there is not a lot of difference between the two logically as posted there - but then you have changed the statements when you posted this (note the then is missing from your if's here). &amp;nbsp;In your previous example:&lt;/P&gt;
&lt;PRE&gt;if ID= lag(ID) and score&amp;gt; lag(score) then flag = 'Increased';
else if ID= lag(ID) and score&amp;lt; lag(score) then flag = 'Decreased';
else if ID= lag(ID) then flag = 'No Change' 
else flag = '';&lt;/PRE&gt;
&lt;P&gt;The id=lag(id) part was duplicated several times. &amp;nbsp;That is one point. &amp;nbsp;The second point is on readability, so for two options then its not much difference, however when you have lots of conditions, it is far easier to read the select statement rather than a lot of if/if else statements, you can also follow down the page where the breaks. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253407#M48220</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-01T11:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253408#M48221</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23778"&gt;@troopon﻿&lt;/a&gt;&amp;nbsp;your original program doesn't work for the same reason that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;&amp;nbsp;program does not work. &amp;nbsp;You are doing conditional lag which does not work in this situation. &amp;nbsp;LAG is not last OBS it is last PUSH.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 11:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253408#M48221</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-03-01T11:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253423#M48226</link>
      <description>&lt;P&gt;Yes, it worked for the small test data I had, so didn't look at it further. &amp;nbsp;I only really wanted to show the select statement.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 13:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253423#M48226</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-01T13:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253530#M48267</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23778"&gt;@troopon&lt;/a&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Given your sample data, the code and output shown in your initial post are not consistent (even after adding the missing semicolon in the second ELSE IF statement and correcting the case mismatch "Change" vs. "change"): Contrary to your statement, the last observation would receive FLAG='Increased'. The first IF condition is processed for every observation. Hence, for the last observation it evaluates to &lt;FONT face="courier new,courier"&gt;'B'='B' and 30&amp;gt;20&lt;/FONT&gt;, which is true.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__&lt;/a&gt;&amp;nbsp;has pointed out already, your code doesn't work correctly, because&lt;/LI&gt;
&lt;/OL&gt;
&lt;UL&gt;
&lt;LI&gt;some of the LAG function calls are conditional (ELSE branches are not executed if a previous IF condition was met!). This&amp;nbsp;means, depending on the input data, it can happen that they are not executed for a particular observation. AND&lt;/LI&gt;
&lt;LI&gt;The intention is (apparently) that &lt;FONT face="courier new,courier"&gt;lag(&lt;EM&gt;variable&lt;/EM&gt;)&lt;/FONT&gt;&amp;nbsp;should give the value of &lt;EM&gt;&lt;FONT face="courier new,courier"&gt;variable&lt;/FONT&gt;&lt;/EM&gt; of the previous observation. But this is not the case if the function was not executed for the previous observation.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;With your sample data, however, this error is not visible in the output.&amp;nbsp;In contrast, with data_null__'s sample data it would become apparent, as shown below.&lt;/P&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="output.png" style="width: 313px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29131iB167E0539D7BFB2D/image-size/large?v=v2&amp;amp;px=999" role="button" title="output.png" alt="output.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;(EDIT: The sort order of this test data set might not be what you want in practice, but it doesn't interfere with my explanations of the LAG function.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The third observation of ID C is incorrectly flagged "No Change," because for the previous observation the first IF condition was met. As a consequence, the queue behind the second &lt;FONT face="courier new,courier"&gt;lag(score)&lt;/FONT&gt;&amp;nbsp;was not touched. So, when the third obs. of ID C is processed, it still contains the value from the &lt;EM&gt;first&lt;/EM&gt; obs. of ID C. The condition 'C'='C' and 20=20 (comparing values from the first and third observation of ID C) yields the incorrect flag.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think, two very important points for understanding the LAG function are that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;for each place where the LAG function occurs in the code a separate queue is created&lt;/LI&gt;
&lt;LI&gt;if and only if the function call is executed, the LAG function returns the value from the queue and writes the current value of the variable in its argument to the queue.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Please note that the DIF function is closely related to the LAG function and therefore requires similar attention.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As mohamed_zaki has mentioned, variable DATE has no impact on variable FLAG as it is not involved in its definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[Edit 2019-04-30: Reattached screenshot, which had been deleted by mistake.]&lt;/P&gt;</description>
      <pubDate>Tue, 30 Apr 2019 17:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253530#M48267</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-30T17:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function help</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253535#M48271</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;:&amp;nbsp;Your code introduces conditional execution of LAG functions in a twofold way:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The two LAG function calls in the SELECT statement are not executed in case that the IF condition is false.&lt;/LI&gt;
&lt;LI&gt;The LAG function call in the second WHEN statement is not executed if the previous WHEN condition is true.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The error pointed out by data_null__ is due to item 1.&amp;nbsp;To see the impact of item 2, you can add an 11th observation to data_null__'s sample data with ID='C' and score=20.&amp;nbsp;This observation will be incorrectly flagged "No change" (rather than "Decreased") due to item 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Mar 2016 17:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-help/m-p/253535#M48271</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-01T17:51:39Z</dc:date>
    </item>
  </channel>
</rss>

