<?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: Duplicates by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263492#M269227</link>
    <description>&lt;P&gt;Sort the data, then drop not first records, then sort back into original order - note I set a variable N to keep original order:&lt;/P&gt;
&lt;PRE&gt;data have;
  input var1 var2 var3 $;
  n=_n_;
cards;
1 1 A
2 1 B
3 1 C
4 2 A
5 2 M
6 2 T
7 3 A
8 3 B
9 3 C
10 4 S
11 4 V
12 4 Z
;
run;

proc sort data=have out=want;
  by var3 var2 var1;
run;

data want;
  set want;
  by var3 var2 var1;
  if not(first.var3) then delete;
run;

proc sort data=want;
  by n;
run;&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Apr 2016 13:02:02 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-04-13T13:02:02Z</dc:date>
    <item>
      <title>Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263488#M269225</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This seems like a very simple problem, but I can't seem to resolve it on my own.&amp;nbsp; I need to delete a group when the groups values are duplicates of another.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1 var2 var3 $;
cards;
1 1 A
2 1 B
3 1 C
4 2 A
5 2 M
6 2 T
7 3 A
8 3 B
9 3 C
10 4 S
11 4 V
12 4 Z
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;Want:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;Var 1 Var2 Var3&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;1 1 A&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;2 1 B&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;3 1 C&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;4 2 A&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;5 2 M&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;6 2 T&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;10 4 S&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;11 4 V&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;12 4 Z&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;&lt;FONT face="Calibri"&gt;How can I delete an entire group if the values between two groups match?&amp;nbsp; (If all values in var3 = all values in var3 by var2 then delete last.var2).&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;I’ve tried sql:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="navy"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;FONT color="black"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="blue"&gt;create table&lt;FONT color="black"&gt; want &lt;/FONT&gt;as&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="blue"&gt;select&lt;FONT color="black"&gt; * &lt;/FONT&gt;from&lt;FONT color="black"&gt; have&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="blue"&gt;group by&lt;FONT color="black"&gt; var3&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;having var2 = min(var2)&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="blue"&gt;order by&lt;FONT color="black"&gt; var1;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT color="navy"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;FONT color="black"&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;And it deletes the duplicate var2 group fine, but it also deletes any duplicate value of var3.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;I’ve also tried proc sort with a data step including nodups and first and last, but I could never get it to come close to my desired output.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Calibri" size="2"&gt;&lt;SPAN style="font-size: 11pt;"&gt;Where am I going wrong?&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 13 Apr 2016 12:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263488#M269225</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T12:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263489#M269226</link>
      <description>I forgot to mention, I am using SAS 9.2.</description>
      <pubDate>Wed, 13 Apr 2016 12:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263489#M269226</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T12:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263492#M269227</link>
      <description>&lt;P&gt;Sort the data, then drop not first records, then sort back into original order - note I set a variable N to keep original order:&lt;/P&gt;
&lt;PRE&gt;data have;
  input var1 var2 var3 $;
  n=_n_;
cards;
1 1 A
2 1 B
3 1 C
4 2 A
5 2 M
6 2 T
7 3 A
8 3 B
9 3 C
10 4 S
11 4 V
12 4 Z
;
run;

proc sort data=have out=want;
  by var3 var2 var1;
run;

data want;
  set want;
  by var3 var2 var1;
  if not(first.var3) then delete;
run;

proc sort data=want;
  by n;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Apr 2016 13:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263492#M269227</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-13T13:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263494#M269228</link>
      <description>Thanks the quick response RW9! I applied your solution and am getting results that not only remove the duplicate group from var2 but also any duplicate var3 values. I will have var3 values that duplicate a lot so need to keep those. Is there a way to restrict the deletion to just the duplicated group?</description>
      <pubDate>Wed, 13 Apr 2016 13:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263494#M269228</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T13:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263495#M269229</link>
      <description>&lt;P&gt;I'm pondering it, but I DO NOT consider this a simple problem! Hopefully, more soon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tom&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 13:11:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263495#M269229</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2016-04-13T13:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263500#M269230</link>
      <description>That's actually very validating to hear Tom!</description>
      <pubDate>Wed, 13 Apr 2016 13:21:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263500#M269230</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T13:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263505#M269231</link>
      <description>&lt;P&gt;Sorry, I can only work with what you post here, show the problem in data. &amp;nbsp;What defines the term group in your statement "&lt;SPAN&gt;Is there a way to restrict the deletion to just the duplicated group"? &amp;nbsp;I had thought by your original post that var3 indicated the group, and thus any subsequent appearances of that variable would be considered "duplicates".&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 13:39:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263505#M269231</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-13T13:39:39Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263509#M269232</link>
      <description>&lt;P&gt;Sorry I wasn't clear.&amp;nbsp; Var2 is the variable that contains the groups (i.e. 1, 2, 3 and 4).&amp;nbsp; Var3 are the values for each group (i.e. Group 1 contains the values A, B and C).&amp;nbsp; I am hoping to find a solution that identifies that&amp;nbsp;groups 1 and 3 both have the same set of values (A, B, C) and to then delete group 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that helps.&amp;nbsp; Does "show the problem in data" mean you would prefer to work with a table that has more relevant data instead of example data?&amp;nbsp; I can put something together if that's the case.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 13:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263509#M269232</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T13:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263515#M269233</link>
      <description>&lt;P&gt;Ah, I see. &amp;nbsp;The way I do it below, is to transpose up the data within each group, sort nodupkey the cat'd columns to get a disintct list, and then remove observations not having a group in the nodupkeylist. &amp;nbsp;Well, you will see the logic if you run it step by step. &amp;nbsp;Will have a think if there is an easier syntax for it.&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; input var1 var2 var3 $;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 A&lt;BR /&gt;2 1 B&lt;BR /&gt;3 1 C&lt;BR /&gt;4 2 A&lt;BR /&gt;5 2 M&lt;BR /&gt;6 2 T&lt;BR /&gt;7 3 A&lt;BR /&gt;8 3 B&lt;BR /&gt;9 3 C&lt;BR /&gt;10 4 S&lt;BR /&gt;11 4 V&lt;BR /&gt;12 4 Z&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=have out=have2;&lt;BR /&gt; by var2 var3;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc transpose data=have2 out=t_have;&lt;BR /&gt; by var2;&lt;BR /&gt; var var3;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data t_have;&lt;BR /&gt; set t_have (drop=_name_);&lt;BR /&gt; all=catx(',',of col:);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=t_have nodupkey;&lt;BR /&gt; by all;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt; delete from HAVE&lt;BR /&gt; where VAR2 not in (select VAR2 from T_HAVE);&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;One good thing about this approach is it removes data from the original table, leaving sort as is.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263515#M269233</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-13T14:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263527#M269234</link>
      <description>&lt;P&gt;Wow thank you RW9!&amp;nbsp; That is a very clever solution!&amp;nbsp; I applied it to my data and it works like a charm.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have time to explain, how did "of col:" in&amp;nbsp;the following code segment remove the duplicated group from var2?&amp;nbsp; It's fascinating.&lt;/P&gt;&lt;P&gt;data t_have;&lt;BR /&gt;set t_have (drop=_name_);&lt;BR /&gt;all=catx(',',of col:);&lt;BR /&gt;run;&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>Wed, 13 Apr 2016 14:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263527#M269234</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T14:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263530#M269235</link>
      <description>&lt;P&gt;You will normally see the of used in conjunction with array statements, so thats probably a good place to start. &amp;nbsp;Say we have:&lt;BR /&gt;array col{5} abc def asd iop yui;&lt;/P&gt;
&lt;P&gt;What this is doing is creating a reference to those variables:&amp;nbsp;&lt;SPAN&gt;abc def asd iop yui.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This reference is col and has 5 elements.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now if I do:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;catx(',',of col{*});&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This wil concatenate all values in the array col with commas.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Much the same in the way I have used it in my code. &amp;nbsp;There is a special syntax for specifying a range of variables with the same prefix, so in my example each variable I am interested in is prefixed with COL, so I can refer to all of them as: COL:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I can refer to this in the same way as if it was an array, so we end up with:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;all=catx(',',of COL:);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Or to use long notation:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;all=catx(',',COL1,COL2,COL3...);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263530#M269235</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-13T14:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263544#M269236</link>
      <description>&lt;P&gt;Thanks for the explanation RW9.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/263544#M269236</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2016-04-13T14:59:46Z</dc:date>
    </item>
    <item>
      <title>Re: Duplicates by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/264298#M269237</link>
      <description>&lt;P&gt;Maybe I've overlooked something, but it seems to me that the data step creating variable ALL could be omitted. Variable ALL could be replaced by variable list COL: in the subsequent PROC SORT step.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Apr 2016 21:43:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Duplicates-by-group/m-p/264298#M269237</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-15T21:43:12Z</dc:date>
    </item>
  </channel>
</rss>

