<?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 Selecting records with latest date in each year in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966253#M83979</link>
    <description>Hi!&lt;BR /&gt;I have the following dataset&lt;BR /&gt;&lt;BR /&gt;Data&amp;nbsp;have;&lt;BR /&gt;input id $&amp;nbsp; dt:yymmdd10. year&amp;nbsp; test&amp;nbsp;$&amp;nbsp; result $;&lt;BR /&gt;format dt&amp;nbsp; date11.;&lt;BR /&gt;datalines;&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20100429 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&amp;nbsp;&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20100712 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20101025 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20110509 2011&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20110909 2011&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150301 2015&amp;nbsp;&amp;nbsp; T2&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150801 2015&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150914 2015&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCGH&amp;nbsp;&amp;nbsp; 20190613 2019&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCGH&amp;nbsp;&amp;nbsp; 20190815 2019&amp;nbsp;&amp;nbsp; T2&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCPK&amp;nbsp;&amp;nbsp; 20130409 2013&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCPK&amp;nbsp;&amp;nbsp; 20131021 2013&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I want to select the record with the latest date in a year for all records with same id and year. I also want if a record that has a result ‘Y’ but not the latest date, it should be prioritised and selected over other records with result ‘N’, same ID and date in later months. In summary, my objective is to have only one record (from same IDs) in a year, if the result variable is ‘Y’ or ‘N’ for all, I want the record with the latest date. Any record (with same ID) with result ‘Y’ in an earlier date should be selected over a record with result ‘N’ in a later date. The output should have only one record for each ID in each year. See below:&lt;BR /&gt;&lt;BR /&gt;Want&lt;BR /&gt;id dt Year test result&lt;BR /&gt;ABCDE 20101025 2010 T1 Y&lt;BR /&gt;ABCDE 20110909 2011 T1 Y&lt;BR /&gt;ABCUK 20150301 2015 T2 Y&lt;BR /&gt;ABCGH 20190815 2019 T2&lt;BR /&gt;ABCPK 20131021 2013 T1&lt;BR /&gt;&lt;BR /&gt;I have tried&lt;BR /&gt;Price sql;&lt;BR /&gt;Create table want as&lt;BR /&gt;Select*&lt;BR /&gt;From have&lt;BR /&gt;Group by id&lt;BR /&gt;Having dt=max(dt);&lt;BR /&gt;Quit;&lt;BR /&gt;&lt;BR /&gt;But it does not give the output based on my specific requirements.&lt;BR /&gt;&lt;BR /&gt;Thank you for your help!&lt;BR /&gt;</description>
    <pubDate>Sun, 11 May 2025 07:13:25 GMT</pubDate>
    <dc:creator>SASPreK</dc:creator>
    <dc:date>2025-05-11T07:13:25Z</dc:date>
    <item>
      <title>Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966253#M83979</link>
      <description>Hi!&lt;BR /&gt;I have the following dataset&lt;BR /&gt;&lt;BR /&gt;Data&amp;nbsp;have;&lt;BR /&gt;input id $&amp;nbsp; dt:yymmdd10. year&amp;nbsp; test&amp;nbsp;$&amp;nbsp; result $;&lt;BR /&gt;format dt&amp;nbsp; date11.;&lt;BR /&gt;datalines;&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20100429 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&amp;nbsp;&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20100712 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20101025 2010&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20110509 2011&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCDE&amp;nbsp;&amp;nbsp; 20110909 2011&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150301 2015&amp;nbsp;&amp;nbsp; T2&amp;nbsp;&amp;nbsp; Y&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150801 2015&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCUK&amp;nbsp;&amp;nbsp; 20150914 2015&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCGH&amp;nbsp;&amp;nbsp; 20190613 2019&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCGH&amp;nbsp;&amp;nbsp; 20190815 2019&amp;nbsp;&amp;nbsp; T2&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCPK&amp;nbsp;&amp;nbsp; 20130409 2013&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;ABCPK&amp;nbsp;&amp;nbsp; 20131021 2013&amp;nbsp;&amp;nbsp; T1&amp;nbsp;&amp;nbsp; N&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I want to select the record with the latest date in a year for all records with same id and year. I also want if a record that has a result ‘Y’ but not the latest date, it should be prioritised and selected over other records with result ‘N’, same ID and date in later months. In summary, my objective is to have only one record (from same IDs) in a year, if the result variable is ‘Y’ or ‘N’ for all, I want the record with the latest date. Any record (with same ID) with result ‘Y’ in an earlier date should be selected over a record with result ‘N’ in a later date. The output should have only one record for each ID in each year. See below:&lt;BR /&gt;&lt;BR /&gt;Want&lt;BR /&gt;id dt Year test result&lt;BR /&gt;ABCDE 20101025 2010 T1 Y&lt;BR /&gt;ABCDE 20110909 2011 T1 Y&lt;BR /&gt;ABCUK 20150301 2015 T2 Y&lt;BR /&gt;ABCGH 20190815 2019 T2&lt;BR /&gt;ABCPK 20131021 2013 T1&lt;BR /&gt;&lt;BR /&gt;I have tried&lt;BR /&gt;Price sql;&lt;BR /&gt;Create table want as&lt;BR /&gt;Select*&lt;BR /&gt;From have&lt;BR /&gt;Group by id&lt;BR /&gt;Having dt=max(dt);&lt;BR /&gt;Quit;&lt;BR /&gt;&lt;BR /&gt;But it does not give the output based on my specific requirements.&lt;BR /&gt;&lt;BR /&gt;Thank you for your help!&lt;BR /&gt;</description>
      <pubDate>Sun, 11 May 2025 07:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966253#M83979</guid>
      <dc:creator>SASPreK</dc:creator>
      <dc:date>2025-05-11T07:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966255#M83980</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you looking for something like the following? If not please clarify what should be different.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* sort in required order */
proc sort data = have
          out  = have_sorted;

  by id year result dt;
run;


/* obtain last record per id &amp;amp; year */
data want;
  set have_sorted;
  
  by id year result dt;
  
  if last.year;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks &amp;amp; kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Sun, 11 May 2025 09:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966255#M83980</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2025-05-11T09:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966256#M83981</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466295"&gt;@SASPreK&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BR /&gt;I have tried&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;Price sql;&lt;/FONT&gt;&lt;BR /&gt;Create table want as&lt;BR /&gt;Select*&lt;BR /&gt;From have&lt;BR /&gt;Group by id&lt;BR /&gt;Having dt=max(dt);&lt;BR /&gt;Quit;&lt;BR /&gt;&lt;BR /&gt;But it does not give the output based on my specific requirements.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;PRICE SQL ??? That won't work. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you didn't tell SQL to do this for each year. How about 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 *
    from have
    group by id,year
    having dt=max(dt);
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>Sun, 11 May 2025 10:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966256#M83981</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-05-11T10:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966261#M83982</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data have;
input id $  dt:yymmdd10. year  test $  result $;
format dt  date11.;
datalines;
ABCDE   20100429 2010   T1   N 
ABCDE   20100712 2010   T1   N
ABCDE   20101025 2010   T1   Y
ABCDE   20110509 2011   T1   Y
ABCDE   20110909 2011   T1   Y
ABCUK   20150301 2015   T2   Y
ABCUK   20150801 2015   T1   N
ABCUK   20150914 2015   T1   N
ABCGH   20190613 2019   T1   N
ABCGH   20190815 2019   T2   N
ABCPK   20130409 2013   T1   N
ABCPK   20131021 2013   T1   N
;
proc sql;
create table part1 as
select * from have where result='Y' group by id,year having dt=max(dt);
create table part2 as
select * from have where catx('|',id,year) not in ( select catx('|',id,year)  from part1) 
 group by id,year having dt=max(dt);
create table want as
select * from part1 union select * from part2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 May 2025 14:12:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966261#M83982</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-05-11T14:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966263#M83983</link>
      <description>&lt;P&gt;PROC SORT / DATA step method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by id year result dt;
run;

data want;
set have;
by id year;
if last.year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 May 2025 17:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/966263#M83983</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-05-11T17:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting records with latest date in each year</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/972494#M84033</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466295"&gt;@SASPreK&lt;/a&gt;&amp;nbsp;,your requirement should be done in several steps using different techniques, it would not be able to achieve in a single step through a single technique (e.g., I tried many times with &lt;EM&gt;retain&lt;/EM&gt; and &lt;EM&gt;if.first /if.last&lt;/EM&gt; techniques but failed and almost give up this question and then switched to other techniques). The steps basically include the follows: (a) produce the final &lt;EM&gt;results column&lt;/EM&gt; according to your requirements, this can be done with &lt;EM&gt;proc transpose&lt;/EM&gt; and&lt;EM&gt; array&lt;/EM&gt;, and (b) output the last &lt;EM&gt;id&lt;/EM&gt; and &lt;EM&gt;year&lt;/EM&gt; row together with the final &lt;EM&gt;results column&lt;/EM&gt;, this can be done though &lt;EM&gt;if.last&lt;/EM&gt;&amp;nbsp; within a &lt;EM&gt;data step&lt;/EM&gt; and then a &lt;EM&gt;sql join&lt;/EM&gt;. The code and results are as follows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $  dt:yymmdd10. year  test $  
      result $;
format dt  date11.;
datalines;
ABCDE   20100429 2010   T1   N 
ABCDE   20100712 2010   T1   N
ABCDE   20101025 2010   T1   Y
ABCDE   20110509 2011   T1   Y
ABCDE   20110909 2011   T1   Y
ABCUK   20150301 2015   T2   Y
ABCUK   20150801 2015   T1   N
ABCUK   20150914 2015   T1   N
ABCGH   20190613 2019   T1   N
ABCGH   20190815 2019   T2   N
ABCPK   20130409 2013   T1   N
ABCPK   20131021 2013   T1   N
;
run;
proc print data=have;run;

/*produce final results column according to
requirements, using transpose and array*/
proc sort data=have out=have1;
   by id dt;
run;
proc print data=have1;run;
proc transpose data=have1 out=have2;
   by id;
   var result;
run;
proc print data=have2;run;
data have3;
   set have2;
   array rst[5] $ col1-col5;
   do i=1 to 5;
      if rst[i]='Y' then result='Y';
   end;
run;
proc print data=have3;run;

/*output the last id and year row together
with the final results, using if.last in
a data step and sql join*/
data have4;
   set have1;
   by id year;
   if last.year;
   drop result;
run;
proc print data=have4;run;
proc sql;
select h4.*,
       h3.result      
   from have4 as h4 left join
        have3 as h3
   on h4.id=h3.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1754999003863.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108961i0E80113020871CCD/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1754999003863.png" alt="dxiao2017_0-1754999003863.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_2-1754999065736.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108963i51C2F7D1779CB459/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_2-1754999065736.png" alt="dxiao2017_2-1754999065736.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_4-1754999251516.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108965iC8F36012A94958AB/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_4-1754999251516.png" alt="dxiao2017_4-1754999251516.png" /&gt;&lt;/span&gt;&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="dxiao2017_5-1754999310883.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/108966iE7C425E867A54D9D/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_5-1754999310883.png" alt="dxiao2017_5-1754999310883.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Aug 2025 11:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Selecting-records-with-latest-date-in-each-year/m-p/972494#M84033</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-08-12T11:52:58Z</dc:date>
    </item>
  </channel>
</rss>

