BookmarkSubscribeRSS Feed
jaulz
Calcite | Level 5

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?

5 REPLIES 5
ballardw
Super User

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;
anoopmohandas7
Quartz | Level 8

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 ;

 

Astounding
PROC Star

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.

 

Peter_C
Rhodochrosite | Level 12
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
Peter_C
Rhodochrosite | Level 12
 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 962 views
  • 0 likes
  • 5 in conversation