Conditional deletions

Reply
New User
Posts: 1

Conditional deletions

I have a dataset with multiple records with the same ID. If a specific condition exists for one of the records, I need to delete all the records with that same ID. How can I do this?

Super User
Posts: 11,343

Re: Conditional deletions

Generally you need to identify the record, select the Id to delete an then do it.

 

Here's one example:

data have;
   input id value;
   if value > 100 then Flag=1;
datalines;
1  10
1  15
1  25
2  12
2  125
2  16
3  .
3  1
;
run;

proc sql;
   create table want as
   select *
   from have
   where id ne (
      select distinct id
      from have
      where flag=1
      );
quit;
Contributor
Posts: 44

Re: Conditional deletions

The output could also be achieved using a merge step.

data want1 want2 ;
   input id value;
   if value > 100 then output want1 ;
   else output want2 ;
datalines;
1  10
1  15
1  130
2  12
2  125
2  16
3  .
3  1
;
run;

data want ;
merge want1 (in=a) want2 (in=b) ;
by id;
if (a=0) and (b=1) ;
run ;

 

Super User
Posts: 5,516

Re: Conditional deletions

Assuming your data set is in order by ID, here is a fairly standard approach:

 

data want;

ID_wanted='Y';

do until (last.id);

   set have;

   by id;

   if    /* some condition goes here */ then ID_wanted='N';

end;

do until (last.id);

   set have;

   by id;

   if ID_wanted='Y' then output;

end;

drop ID_wanted;

run;

 

The top loop lets you examine all observations for an ID, and determine whether you want them or not.  The bottom loop reads in the same observations and outputs if the top loop determines that is appropriate.

 

Valued Guide
Posts: 2,177

Re: Conditional deletions

In a database world, we would delete relevant rows in a SQL update statement something like:
Delete from table where id in( select distinct id from table where relevant condition)
;
I expect SAS SQL offers something similar.
It offers the benefit of speed as the data are updated, not rewritten.
However it is not reversible
Valued Guide
Posts: 2,177

Re: Conditional deletions

 
Ask a Question
Discussion stats
  • 5 replies
  • 143 views
  • 0 likes
  • 5 in conversation