<?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 to keep one observation by group (deleting duplicates by condition) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618655#M181518</link>
    <description>&lt;P&gt;1. SORT approach would require 2 sorts:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input County       year       Count;
cards;
20001       2008           1
20001       2008           2
20001       2008           3
20001       2006           1
20002       2008           1
20002       2008           2
;

proc sort data=have out=temp;
by county year descending count;
run;

proc sort data=temp out=want nodupkey;
by county year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input County       year       Count;
cards;
20001       2008           1
20001       2008           2
20001       2008           3
20001       2006           1
20002       2008           1
20002       2008           2
;

data want;
 set have;
 by county year notsorted;
 if last.year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assuming your dataset order is what it is as shown in your sample, you would need a NOTSORTED option in the BY statement in your datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Proc SQL grouped filter using HAVING clause&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want as
select *
from have
group by county,year
having count=max(count);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jan 2020 18:53:38 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-01-20T18:53:38Z</dc:date>
    <item>
      <title>How to keep one observation by group (deleting duplicates by condition)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618636#M181510</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset looks like below. I want to keep the observations with the highest number in "count" variable by county and year.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;County&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;year&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Count&lt;/P&gt;&lt;P&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;20002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;20002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The outcome that I want has to look like...&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;County&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;year&amp;nbsp; &amp;nbsp; &amp;nbsp; Count&lt;/P&gt;&lt;P&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;20001&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2006&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;20002&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2008&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=rc out=rc_dup nodupkey;
	by county year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jan 2020 18:08:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618636#M181510</guid>
      <dc:creator>cphd</dc:creator>
      <dc:date>2020-01-20T18:08:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep one observation by group (deleting duplicates by condition)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618639#M181512</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by county year count;
run;

data want;
set have;
by county year;
if last.year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jan 2020 18:12:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618639#M181512</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-01-20T18:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep one observation by group (deleting duplicates by condition)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618655#M181518</link>
      <description>&lt;P&gt;1. SORT approach would require 2 sorts:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input County       year       Count;
cards;
20001       2008           1
20001       2008           2
20001       2008           3
20001       2006           1
20002       2008           1
20002       2008           2
;

proc sort data=have out=temp;
by county year descending count;
run;

proc sort data=temp out=want nodupkey;
by county year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input County       year       Count;
cards;
20001       2008           1
20001       2008           2
20001       2008           3
20001       2006           1
20002       2008           1
20002       2008           2
;

data want;
 set have;
 by county year notsorted;
 if last.year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assuming your dataset order is what it is as shown in your sample, you would need a NOTSORTED option in the BY statement in your datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Proc SQL grouped filter using HAVING clause&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want as
select *
from have
group by county,year
having count=max(count);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2020 18:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-one-observation-by-group-deleting-duplicates-by/m-p/618655#M181518</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-20T18:53:38Z</dc:date>
    </item>
  </channel>
</rss>

