<?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: delete contrasting records in Programming 1 and 2</title>
    <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758235#M797</link>
    <description>&lt;P&gt;If the&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The data are sorted by KEY, as they are in your sample&lt;/LI&gt;
&lt;LI&gt;You never have more than one D per key, or more then one A per key&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;then a DATA step with a BY statement will work, by keeping only those KEY's with a single observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
infile datalines delimiter=',';
input key :$50. add_delete $1. ;
datalines;
1234_1234567654321_P_L720,A
1234_1234567654321_P_L738,A
1234_1234567654321_P_L738,D
1234_1234567654321_P_L821,A
1234_1234567654321_P_L821,D
1234_1234567654321_P_R209,A
1234_1234567654321_P_R209,D
1234_7654321234567_P_L720,A
1234_7654321234567_P_L720,D
1234_7654321234567_P_L738,A
1234_7654321234567_P_L738,D
1234_7654321234567_P_L821,D
1234_7654321234567_P_R209,A
1234_7654321234567_P_R209,D
;
data want;
  set have;
  by key;
  if first.key=1 and last.key=1;
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;Alternatively, using a condition more analogous to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s&amp;nbsp; suggestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have (where=(add_delete='A') in=ina)
        have (where=(add_delete='D') in=ind);
  by key;
  where ina=0 or ind=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which just says to keep those KEY's in which either A never appears or D never appears.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For large datasets, this may be faster than the SQL solution because it only compares contiguous records for matching KEYs.&amp;nbsp; But again, it requires the data to be sorted by KEY.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jul 2021 19:37:56 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-07-29T19:37:56Z</dc:date>
    <item>
      <title>delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758211#M793</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to remove rows where key is the same and add_delete is both "A" and "D"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
infile datalines delimiter=',';
input key $50. add_delete $1. ;
datalines;
1234_1234567654321_P_L720,A
1234_1234567654321_P_L738,A
1234_1234567654321_P_L738,D
1234_1234567654321_P_L821,A
1234_1234567654321_P_L821,D
1234_1234567654321_P_R209,A
1234_1234567654321_P_R209,D
1234_7654321234567_P_L720,A
1234_7654321234567_P_L720,D
1234_7654321234567_P_L738,A
1234_7654321234567_P_L738,D
1234_7654321234567_P_L821,D
1234_7654321234567_P_R209,A
1234_7654321234567_P_R209,D
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output of this example should be&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="264"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="186"&gt;key&lt;/TD&gt;
&lt;TD width="78"&gt;add_delete&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1234_1234567654321_P_L720&lt;/TD&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1234_7654321234567_P_L821&lt;/TD&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It might be as simple as counting the occurrences by key and if it's greater than 1, delete. I'm testing that now. Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 19:04:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758211#M793</guid>
      <dc:creator>kb011235</dc:creator>
      <dc:date>2021-07-29T19:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758220#M794</link>
      <description>Recode A to 1 and D to -1. &lt;BR /&gt;Then sum it by the KEY. Anything that is 0 gets deleted.</description>
      <pubDate>Thu, 29 Jul 2021 19:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758220#M794</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-29T19:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758221#M795</link>
      <description>But _L720 also has both 'A' and 'D' for same Key.&lt;BR /&gt;&lt;BR /&gt;1234_7654321234567_P_L720,A&lt;BR /&gt;1234_7654321234567_P_L720,D</description>
      <pubDate>Thu, 29 Jul 2021 19:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758221#M795</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-07-29T19:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758229#M796</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
infile datalines dsd;
length key $50 add_delete $1;
input key add_delete;
datalines;
1234_1234567654321_P_L720,A
1234_1234567654321_P_L738,A
1234_1234567654321_P_L738,D
1234_1234567654321_P_L821,A
1234_1234567654321_P_L821,D
1234_1234567654321_P_R209,A
1234_1234567654321_P_R209,D
1234_7654321234567_P_L720,A
1234_7654321234567_P_L720,D
1234_7654321234567_P_L738,A
1234_7654321234567_P_L738,D
1234_7654321234567_P_L821,D
1234_7654321234567_P_R209,A
1234_7654321234567_P_R209,D
;

proc sql;
select 
    * 
from have as a
where not exists (select * from have as b where a.key=b.key and a.add_delete ne b.add_delete);
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-1627586196973.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62101i92F7260D146FDDD4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1627586196973.png" alt="PGStats_0-1627586196973.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 19:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758229#M796</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-07-29T19:16:52Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758235#M797</link>
      <description>&lt;P&gt;If the&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The data are sorted by KEY, as they are in your sample&lt;/LI&gt;
&lt;LI&gt;You never have more than one D per key, or more then one A per key&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;then a DATA step with a BY statement will work, by keeping only those KEY's with a single observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
infile datalines delimiter=',';
input key :$50. add_delete $1. ;
datalines;
1234_1234567654321_P_L720,A
1234_1234567654321_P_L738,A
1234_1234567654321_P_L738,D
1234_1234567654321_P_L821,A
1234_1234567654321_P_L821,D
1234_1234567654321_P_R209,A
1234_1234567654321_P_R209,D
1234_7654321234567_P_L720,A
1234_7654321234567_P_L720,D
1234_7654321234567_P_L738,A
1234_7654321234567_P_L738,D
1234_7654321234567_P_L821,D
1234_7654321234567_P_R209,A
1234_7654321234567_P_R209,D
;
data want;
  set have;
  by key;
  if first.key=1 and last.key=1;
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;Alternatively, using a condition more analogous to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s&amp;nbsp; suggestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have (where=(add_delete='A') in=ina)
        have (where=(add_delete='D') in=ind);
  by key;
  where ina=0 or ind=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which just says to keep those KEY's in which either A never appears or D never appears.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For large datasets, this may be faster than the SQL solution because it only compares contiguous records for matching KEYs.&amp;nbsp; But again, it requires the data to be sorted by KEY.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jul 2021 19:37:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758235#M797</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-29T19:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758381#M798</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
infile datalines dsd;
length key $50 add_delete $1;
input key add_delete;
datalines;
1234_1234567654321_P_L720,A
1234_1234567654321_P_L738,A
1234_1234567654321_P_L738,D
1234_1234567654321_P_L821,A
1234_1234567654321_P_L821,D
1234_1234567654321_P_R209,A
1234_1234567654321_P_R209,D
1234_7654321234567_P_L720,A
1234_7654321234567_P_L720,D
1234_7654321234567_P_L738,A
1234_7654321234567_P_L738,D
1234_7654321234567_P_L821,D
1234_7654321234567_P_R209,A
1234_7654321234567_P_R209,D
;

proc sql;
select 
    * 
from have as a
group by key
having count(distinct add_delete)=1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Jul 2021 13:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/758381#M798</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-07-30T13:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/759063#M801</link>
      <description>&lt;P&gt;My first try was to use a similar datastep which didn't work:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by key;
  if first.add_delete='A' and last.add_delete='D' then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can you help me understand what's wrong with this setup?&lt;/P&gt;</description>
      <pubDate>Tue, 03 Aug 2021 15:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/759063#M801</guid>
      <dc:creator>kb011235</dc:creator>
      <dc:date>2021-08-03T15:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: delete contrasting records</title>
      <link>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/759084#M802</link>
      <description>Hi:&lt;BR /&gt;  The problem with this logic is that the FIRST.byvar and LAST.byvar values are only ever 0 or 1 (numbers). And your BY statement in the code  is BY KEY; so your program is creating FIRST.KEY and LAST.KEY. But you have coded FIRST.ADD_DELETE and LAST.ADD_DELETE, which should result in messages in the log something like: "NOTE: Invalid numeric data" -- because of the fact that FIRST. and LAST. variables are numeric variables but you are testing for values of 'A' and 'D'.&lt;BR /&gt;  And if you intend to use FIRST. and LAST. variables in your DATA step program, ALL the BY variables must be listed in the BY statement.&lt;BR /&gt;Cynthia&lt;BR /&gt;</description>
      <pubDate>Tue, 03 Aug 2021 16:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Programming-1-and-2/delete-contrasting-records/m-p/759084#M802</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-08-03T16:55:53Z</dc:date>
    </item>
  </channel>
</rss>

