<?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: Find the last row with some condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800277#M314767</link>
    <description>&lt;P&gt;Original data set needs a flag. &lt;span class="lia-unicode-emoji" title=":chequered_flag:"&gt;🏁&lt;/span&gt; Okay&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you could merge the data set from my code back in with the original data set (and then optionally delete the data set from my code). Or this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select a.*,a.testdate=max(b.testdate) as flag
    from have as a left join have(where=(testdate&amp;lt;date)) as b on a.id=b.id and a.testdate=b.testdate
    group by b.id
    order by a.id,a.testdate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Mar 2022 20:32:08 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-03-04T20:32:08Z</dc:date>
    <item>
      <title>Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800272#M314763</link>
      <description>&lt;P&gt;I would like to find the last row where testdate is earlier than date within each group. Thanks&lt;/P&gt;&lt;P&gt;Data like below:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;TESTDATE&amp;nbsp; &amp;nbsp; &amp;nbsp; DATE&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp;Nov 20, 2019&amp;nbsp; &amp;nbsp;Nov 25, 2019&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp;Nov 30, 2019&amp;nbsp; &amp;nbsp;Nov 25, 2019&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp;Dec 1, 2019&amp;nbsp; &amp;nbsp; &amp;nbsp;Nov 25, 2019&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp;Nov 1, 2019&amp;nbsp; &amp;nbsp; Nov 18, 2019&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp;Nov 13, 2019&amp;nbsp; &amp;nbsp;Nov 18, 2019&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp;Nov 24, 2019&amp;nbsp; &amp;nbsp;Nov 18, 2019&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 19:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800272#M314763</guid>
      <dc:creator>JillChen0131</dc:creator>
      <dc:date>2022-03-04T19:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800273#M314764</link>
      <description>&lt;P&gt;Is date always the same for each record in an ID? Is the testdate always sorted in chronological order? Is "last row" the same as highest value of testdate?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have(where=(testdate&amp;lt;date));
    by id;
    if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If those assumptions are not valid, then please explain the organization of the data in a lot more detail.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 20:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800273#M314764</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-04T20:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800275#M314765</link>
      <description>&lt;P&gt;answer are all "YES" for your questions. What if I want to flag that record in the original dataset? Should I just merge the new dataset and original? I came out this way when I was doing the programming. Is there any other way without creating a new dataset and merge back?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 20:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800275#M314765</guid>
      <dc:creator>JillChen0131</dc:creator>
      <dc:date>2022-03-04T20:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800277#M314767</link>
      <description>&lt;P&gt;Original data set needs a flag. &lt;span class="lia-unicode-emoji" title=":chequered_flag:"&gt;🏁&lt;/span&gt; Okay&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, you could merge the data set from my code back in with the original data set (and then optionally delete the data set from my code). Or this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table want as select a.*,a.testdate=max(b.testdate) as flag
    from have as a left join have(where=(testdate&amp;lt;date)) as b on a.id=b.id and a.testdate=b.testdate
    group by b.id
    order by a.id,a.testdate;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 20:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800277#M314767</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-04T20:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800283#M314770</link>
      <description>&lt;P&gt;Use the auto-remerge feature of proc SQL to do this with a single query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  (TESTDATE DATE) (:date9.);
format TESTDATE DATE yymmdd10.;
datalines;
A   20nov2019  25nov2019
A   30nov2019  25nov2019
A   01dec2019  25nov2019
B   01nov2019  18nov2019
B   13nov2019  18nov2019
B   24nov2019  18nov2019
;

proc sql;
create table want as
select * 
from have
where testdate lt date
group by id
having testdate = max(testdate);
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1646427950044.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69164i16C97C1106850D62/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1646427950044.png" alt="PGStats_0-1646427950044.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Mar 2022 21:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800283#M314770</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-03-04T21:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800297#M314776</link>
      <description>&lt;P&gt;Or via a Sort and data step - but I'd be using the SQL as proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  (TESTDATE DATE) (:date9.);
format TESTDATE DATE yymmdd10.;
datalines;
A   20nov2019  25nov2019
A   30nov2019  25nov2019
A   01dec2019  25nov2019
B   01nov2019  18nov2019
B   13nov2019  18nov2019
B   24nov2019  18nov2019
;

proc sort data=have(where=(testdate&amp;lt;date)) out=want;
 by id date testdate;
run;

data want;
  set want;
  by id;
  if last.date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Mar 2022 23:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800297#M314776</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-04T23:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800304#M314781</link>
      <description>&lt;P&gt;If I understand correctly, and - I believe you confirmed - (1) data are sorted by &lt;EM&gt;&lt;STRONG&gt;id/testdate&lt;/STRONG&gt;&lt;/EM&gt;, (2) each id has a constant DATE, then you merely need to look ahead one obs to see whether the current obs satisfies the testdate&amp;lt;date condition and the next obs violates the condition:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  (TESTDATE DATE) (:date9.);
format TESTDATE DATE yymmdd10.;
datalines;
A   20nov2019  25nov2019
A   30nov2019  25nov2019
A   01dec2019  25nov2019
B   01nov2019  18nov2019
B   13nov2019  18nov2019
B   24nov2019  18nov2019
;
data want (drop=nxt_:);
  set have (keep=id);
  by id;
  merge have have (firstobs=2 keep=testdate rename=(testdate=nxt_testdate));
  if testdate&amp;lt;date and (last.id=1 or nxt_testdate&amp;gt;=date) then flag=1;
  else flag=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By looking ahead (using firstobs=2 in the second argument of the MERGE statement), you avoid the need to re-merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you don't have a constant DATE value for a given ID, then a simple lookahead doesn't work.&amp;nbsp; Instead you have to read each ID twice - the first time to scan all obs and identify the last (physically last) record satisfying the condition.&amp;nbsp; The second time to output the results with flag=1 in the correct observation.&amp;nbsp; In this case the data need to be grouped only by ID, no secondary sort key needed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (in=firstpass) have (in=secondpass);
  by id;
  _nf+firstpass;
  retain _last_qualifier;
  if firstpass and testdate&amp;lt;date then _last_qualifier=_nf ;
  if secondpass;
  _ns+1;
  flag=(_ns=_last_qualifier);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Mar 2022 04:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800304#M314781</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-05T04:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Find the last row with some condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800320#M314794</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Or via a Sort and data step - but I'd be using the SQL as proposed by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID $  (TESTDATE DATE) (:date9.);
format TESTDATE DATE yymmdd10.;
datalines;
A   20nov2019  25nov2019
A   30nov2019  25nov2019
A   01dec2019  25nov2019
B   01nov2019  18nov2019
B   13nov2019  18nov2019
B   24nov2019  18nov2019
;

proc sort data=have(where=(testdate&amp;lt;date)) out=want;
 by id date testdate;
run;

data want;
  set want;
  by id;
  if last.date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Already proposed, and furthermore the OP states that she wants all the records in the original data set and a new variable flag to indicate which is 1 if this is the record of interest and 0 otherwise.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Mar 2022 10:12:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-the-last-row-with-some-condition/m-p/800320#M314794</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-03-05T10:12:10Z</dc:date>
    </item>
  </channel>
</rss>

