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.
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;
proc sql;
delete from A
where id in (select id from B);
quit;
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;
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.
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.