<?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: How can I extract the first row where the last value in a certain group appears? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782598#M249500</link>
    <description>&lt;P&gt;This seems to be working fine. Thank you!&lt;/P&gt;</description>
    <pubDate>Fri, 26 Nov 2021 21:33:42 GMT</pubDate>
    <dc:creator>aalluru</dc:creator>
    <dc:date>2021-11-26T21:33:42Z</dc:date>
    <item>
      <title>How can I extract the first row where the last value in a certain group appears?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782593#M249496</link>
      <description>&lt;P&gt;Suppose I have the following dataset :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp;c&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if I consider the a=1 group, the last record has c=2. I want to pull the record where c=2 appears in the a=1 group for the first time i.e. 1 3 2. Similarly for the a=2 group, the last record has c=3. Since that is the first time it appears in this group that would be the row to display in that group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the output should be&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; &amp;nbsp;b&amp;nbsp; &amp;nbsp;c&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone please help me out with this? I would really appreciate it.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 20:38:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782593#M249496</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-11-26T20:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: How can I extract the first row where the last value in a certain group appears?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782596#M249498</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
input A  B  C;
rownum = _N_;
cards;
1   1   1
1   2   3
1   3   2
1   4   2
2   4   1
2   3   3
;
run;

data work.lastC_in_group(drop=B);
 set work.have;
 by A;
 if last.A then output;
run;

PROC SQL noprint;
 create table work.want as
 select t1.*
 from   work.have as t1
      , work.lastC_in_group as t2
 where t1.A = t2.A and t1.C = t2.C 
 order by t1.rownum ;
QUIT;

data work.want;
 set work.want;
 by A;
 if first.A then output;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 20:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782596#M249498</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-26T20:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: How can I extract the first row where the last value in a certain group appears?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782598#M249500</link>
      <description>&lt;P&gt;This seems to be working fine. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 26 Nov 2021 21:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782598#M249500</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-11-26T21:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I extract the first row where the last value in a certain group appears?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782651#M249512</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input A  B  C;
cards;
1   1   1
1   2   3
1   3   2
1   4   2
2   4   1
2   3   3
;
run;

data want;
do until(last.c);
 set have;
 by a c notsorted;
 if last.a then found=1;
end;
do until(last.c);
 set have;
 by a c notsorted;
 if found and first.c then output;
end;
drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Nov 2021 10:06:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-extract-the-first-row-where-the-last-value-in-a/m-p/782651#M249512</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-11-27T10:06:39Z</dc:date>
    </item>
  </channel>
</rss>

