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

Hi,

 

I want to remove observation from A which are present in B;

 

 

data a;
input st_code Addr$ Name$ ID;
datalines;
06111 kota atul 552927
08888 delh gaur 552927
13089 pune pink 666666
;
run;


data b;
input st_code ID;
datalines;
06111 552927
13089 552927
13135 552927
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
kiranv_
Rhodochrosite | Level 12

just another way

proc sql;
create table abc as 
select * from a tbl1
where not exists
               (select * from b tbl2
			   where tbl1.id =tbl2.id
               and tbl1.st_code = tbl2.st_code);
quit;
			 

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

What variables do you want this selection to depend on? Both st_code and ID?

 

If so

 

data a;
input st_code Addr$ Name$ ID;
datalines;
06111 kota atul 552927
08888 delh gaur 552927
13089 pune pink 666666
;
run;

data b;
input st_code ID;
datalines;
06111 552927
13089 552927
13135 552927
;
run;

proc sort data = a;
	by ID st_code;
run;

proc sort data = b;
	by ID st_code;
run;

data a;
	merge a b(in=in_b);
	by ID st_code;
	if not in_b then output;
run;
Jagadishkatam
Amethyst | Level 16

Please sort a and b datasets on st_code and id and then merge

 

data want;
merge a(in=a) b(in=b);
by st_code id;
if b and not a;
run;
Thanks,
Jag
atul_desh
Quartz | Level 8

Is there is any other, I tried this one before but it is not removing all the observation from A, which are present in B (on basis of both variable st_code & ID).

 

I think its due to  I've multiple observation of st_code & multiple IDs 

PeterClemmensen
Tourmaline | Level 20

Please post what you want your final dataset to look like, because the way I see it, the only observation from A, which is also in B (based on both st_code and ID) is the first observation in A, which is removed when you run the code above.

Jagadishkatam
Amethyst | Level 16

proc sql is an alternative

 

data a;
input st_code Addr$ Name$ ID;
datalines;
06111 kota atul 552927
08888 delh gaur 552927
13089 pune pink 666666
;
run;

data b;
input st_code ID;
datalines;
06111 552927
13089 552927
13135 552927
;
run;

proc sql;
create table test as select distinct a.* from a right join b on a.st_code^=b.st_code and a.id^=b.id having a.id ne .;
quit;
Thanks,
Jag
kiranv_
Rhodochrosite | Level 12

just another way

proc sql;
create table abc as 
select * from a tbl1
where not exists
               (select * from b tbl2
			   where tbl1.id =tbl2.id
               and tbl1.st_code = tbl2.st_code);
quit;
			 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1934 views
  • 0 likes
  • 4 in conversation