<?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: Using conditional lag to copy values from a previous row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635471#M188681</link>
    <description>&lt;P&gt;You don't need to use LAG.&amp;nbsp; Use BY group processing and RETAIN instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input MemberID $ AdmitID $ ;
cards;
AAA 001  
AAA 001  
AAA 001  
AAA 002
AAA 002  
AAA 002
;

data want;
  set have ;
  by memberid admitid;
  length Maternity $8 ;
  if first.memberid then Maternity=' ';
  else if first.admitid then Maternity='Ignore';
  retain Maternity;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       Member    Admit
Obs      ID       ID      Maternity

 1      AAA       001
 2      AAA       001
 3      AAA       001
 4      AAA       002      Ignore
 5      AAA       002      Ignore
 6      AAA       002      Ignore
&lt;/PRE&gt;</description>
    <pubDate>Sat, 28 Mar 2020 02:23:32 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-03-28T02:23:32Z</dc:date>
    <item>
      <title>Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635218#M188580</link>
      <description>&lt;P&gt;I am using the lag function to identify when the same MemberID has a new AdmitID (002) and creating "Ignore" flag on that line. This part works for the first instance of the new AdmitID but I need all lines that are AdmitID = 002 to also say ignore as I am conditionally summing the data later. I tried adding a second if statement but have had no luck copying down the "ignore" where AdmitID = 002. Any help or suggestions for a better approach are appreciated/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want; set have;&lt;BR /&gt;format maternity $char8.;&lt;BR /&gt;&lt;BR /&gt;if MemberID = lag(MemberID)&lt;BR /&gt;and AdmitID ^= lag(AdmitID)&lt;BR /&gt;and dt_incurred &amp;gt;= lag(dt_adm)&lt;BR /&gt;and dt_incurred &amp;lt;= lag(dt_dischg) then Maternity = "Ignore";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if lag(Maternity) = "Ignore" and AdmitID=lag(AdmitID) then Maternity=lag(Maternity);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I am getting:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MemberID&lt;/TD&gt;&lt;TD&gt;AdmitID&lt;/TD&gt;&lt;TD&gt;Maternity&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;Ignore&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but this is what I am after:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;MemberID&lt;/TD&gt;&lt;TD&gt;AdmitID&lt;/TD&gt;&lt;TD&gt;Maternity&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;001&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;Ignore&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;Ignore&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;AAA&lt;/TD&gt;&lt;TD&gt;002&lt;/TD&gt;&lt;TD&gt;Ignore&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 06:20:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635218#M188580</guid>
      <dc:creator>zmalone91</dc:creator>
      <dc:date>2020-03-27T06:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635225#M188585</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set HAVE;
  format MATERNITY $char8.;
  if MEMBERID     = lag(MEMBERID)
   &amp;amp; ADMITID     ^= lag(ADMITID)
 /*  &amp;amp; DT_INCURRED &amp;gt;= lag(DT_ADM)
   &amp;amp; DT_INCURRED &amp;lt;= lag(DT_DISCHG) */then do;
     IGNORE+1;
     MATERNITY = "Ignore";
   end;  
   if MEMBERID ne lag(MEMBERID) then IGNORE=0; 
   if IGNORE and ADMITID = lag(ADMITID)  then Maternity='Ignore';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 07:36:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635225#M188585</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-03-27T07:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635244#M188592</link>
      <description>Don't use LAG inside condition: ...then Maternity=lag(Maternity);...&lt;BR /&gt;LAG return value from previos LAG calling, not from previos observation.</description>
      <pubDate>Fri, 27 Mar 2020 09:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635244#M188592</guid>
      <dc:creator>Alexandr</dc:creator>
      <dc:date>2020-03-27T09:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635262#M188604</link>
      <description>&lt;P&gt;"Conditional lag" immediately rings a very loud alarm bell.&lt;/P&gt;
&lt;P&gt;The LAG() functions build a FIFO queue where a new element is added ONLY when the function is &lt;EM&gt;actually called&lt;/EM&gt;, not when a data step iteration is executed or an observation is read or written.&lt;/P&gt;
&lt;P&gt;The proper method for conditionally getting lagged values is to always (unconditionally) store the result of the function in a temporary variable (that you drop later), and use this variable in your conditional branch:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;l_Maternity = lag(Maternity);
if l_Maternity = "Ignore" and AdmitID = lag(AdmitID) then Maternity = l_Maternity;
drop l_Maternity;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Using the function in a condition is not critical, as SAS does not optimize the conditions. In the above example, the lag(AdmitID) will be executed even if l_Maternity is not equal to "Ignore".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the sake of efficiency (read: execution speed), it is always better to execute the function only once for each needed variable in a data step and use the temporary variables. Function calls are costly in terms of CPU cycles.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Mar 2020 10:41:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635262#M188604</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-27T10:41:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635388#M188653</link>
      <description>&lt;P&gt;You should not use LAG for this. Retain the Maternity variable instead, much easier:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
  set have;
  by MemberID AdmitID notsorted;
  format maternity $char8.;
  retain maternity;
  if not first.MemberID and first.AdmitID
    and dt_incurred &amp;gt;= lag(dt_adm)
    and dt_incurred &amp;lt;= lag(dt_dischg) then Maternity = "Ignore";
  else if first.AdmitID then Maternity=' ';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead of checking the ID variables with LAG, just use BY and FIRST. I put in a NOTSORTED option on the by statement in case the data is actually sorted by something else.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code looks a bit ineffective, as we check twice for the same condition (first.AdmitID), but as others have noted, it is important to make sure that the LAG function is called for every single observation.&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;</description>
      <pubDate>Fri, 27 Mar 2020 17:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635388#M188653</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-03-27T17:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using conditional lag to copy values from a previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635471#M188681</link>
      <description>&lt;P&gt;You don't need to use LAG.&amp;nbsp; Use BY group processing and RETAIN instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input MemberID $ AdmitID $ ;
cards;
AAA 001  
AAA 001  
AAA 001  
AAA 002
AAA 002  
AAA 002
;

data want;
  set have ;
  by memberid admitid;
  length Maternity $8 ;
  if first.memberid then Maternity=' ';
  else if first.admitid then Maternity='Ignore';
  retain Maternity;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       Member    Admit
Obs      ID       ID      Maternity

 1      AAA       001
 2      AAA       001
 3      AAA       001
 4      AAA       002      Ignore
 5      AAA       002      Ignore
 6      AAA       002      Ignore
&lt;/PRE&gt;</description>
      <pubDate>Sat, 28 Mar 2020 02:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-conditional-lag-to-copy-values-from-a-previous-row/m-p/635471#M188681</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-28T02:23:32Z</dc:date>
    </item>
  </channel>
</rss>

