BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dapenDaniel
Obsidian | Level 7

Hello. I have two data sets. In the Dataset A, an ID has multiple observations. Dataset B is a list of ID that I need to delete from Dataset A. The following is an example.

 

Dataset A                                                          Dataset B

ID              Observations                                         ID

aa                     a11                                                  aa

aa                     a12                                                  bb

aa                     a13

bb                     b12

bb                     b22

cc                     c45

cc                     c56

 

I want to delete the observations which have the same ID with dataset B from Dataset A. What code do I need? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Alternatively in a data step by merge statement

 

data want;
merge A(in=a) B(in=b);
by id;
if a and not b;
run;
Thanks,
Jag

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21
proc sql;
delete from A
where id in (select id from B);
quit;
PG
novinosrin
Tourmaline | Level 20

Don't pay much attention to this. I'm trying to learn indexes, that's all

 


data a;
input id;
cards;
1
2
2
3
3
3
;

data b;
input id;
cards;
3
;


proc sql;
create index id on a (id);
quit;

data a; 
set b; 
do until (_iorc_=%sysrc(_dsenom));
modify a key=id; 
select (_iorc_); 
when(%sysrc(_sok)) do; /* A match was found, update master */
remove;
end;
when (%sysrc(_dsenom)) do; /* No match was found */
_error_ = 0;
end; 
otherwise do; 
length errormessage $200.; 
errormessage = iorcmsg();
put "ATTENTION: unknown error condition: " errormessage;
end;
end;
end;
run;
Reeza
Super User
proc sql;
create table want as
select *
from tableA
where ID not in (select * from tableB);
quit;

@dapenDaniel wrote:

Hello. I have two data sets. In the Dataset A, an ID has multiple observations. Dataset B is a list of ID that I need to delete from Dataset A. The following is an example.

 

Dataset A                                                          Dataset B

ID              Observations                                         ID

aa                     a11                                                  aa

aa                     a12                                                  bb

aa                     a13

bb                     b12

bb                     b22

cc                     c45

cc                     c56

 

I want to delete the observations which have the same ID with dataset B from Dataset A. What code do I need? Thanks.


 

Jagadishkatam
Amethyst | Level 16

Alternatively in a data step by merge statement

 

data want;
merge A(in=a) B(in=b);
by id;
if a and not b;
run;
Thanks,
Jag

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 4271 views
  • 4 likes
  • 5 in conversation