BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
Two datasets A and B have Id's.How to find the id's that are NOT matching between them.

I could use a merge:
if A and NOT B then Output A1;
if NOT A and B then output B1;

Then combine A1 and B1 and get distinct ids.
Is there any other way?
5 REPLIES 5
ieva
Pyrite | Level 9
You can also simply combile both of thes IF statements in one, so no need to merge afterwards:

if (a and not b) or ( b and not a);
SASPhile
Quartz | Level 8
Thanks Ieva
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello SASPhile,

You can use even more simple logic: if NOT (a and b); like this:
[pre]
data a;
x=1; output;
x=2; output;
run;
data b;
x=3; output;
x=2; output;
run;
proc sort data=a;
by x;
run;
proc sort data=b;
by x;
run;
data dif;
merge a (in=a) b(in=b);
if not (a and b);
by x;
run;
[/pre]
Sincerely,
SPR
SASPhile
Quartz | Level 8
Thanks SPR!
Ksharp
Super User
Yes.
There is another way.Try to use proc sql + except .


[pre]
data temp;
set sashelp.class;
where sex eq 'F';
run;
proc sql;
select *
from sashelp.class as a
where a.sex not in (
select b.sex
from temp as b
) ;quit;
[/pre]


But it is low efficient for the sub-query.


Ksharp Message was edited by: Ksharp

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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