DATA Step, Macro, Functions and more

Not common ids

Reply
Super Contributor
Posts: 673

Not common ids

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?
Frequent Contributor
Posts: 82

Re: Not common ids

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);
Super Contributor
Posts: 673

Re: Not common ids

Thanks Ieva
Super Contributor
Super Contributor
Posts: 365

Re: Not common ids

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
Super Contributor
Posts: 673

Re: Not common ids

Thanks SPR!
Super User
Posts: 10,035

Re: Not common ids

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
Ask a Question
Discussion stats
  • 5 replies
  • 186 views
  • 0 likes
  • 4 in conversation