Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Proc SQL to find all the records that only exist in one table but not the other?

Tags:

sql

sas

I'm trying to do this in Enterprise Guide, with a task, otherwise I would just use a data step.

In a data step, this would be:

data names;
 input name $;
 datalines;
  John
  Mary
  Sally
  Fred
  Paul
 ;
run;

data check;
 input name $;
 datalines;
  Mary
  Fred
 ;

Proc sort data=names; by name; run;
Proc sort data=check; by name; run;

Data work.not_in_check;
 merge names(in=n) check(in=c);
 by name;
 if n and not c;
run;
like image 357
Jay Corbett Avatar asked Jan 20 '12 19:01

Jay Corbett


2 Answers

Here's one way. There are surely many others.

proc sql;
 create table not_in_check as
 select name
 from names
 where name not in (select name from check);
quit;
like image 193
itzy Avatar answered Oct 17 '22 16:10

itzy


Another slight variation is:

proc sql;
create table not_in_check as select 
 a.* from names as a left join 
          check as b on
          a.name=b.name
          where b.name is null;
quit;
like image 7
Longfish Avatar answered Oct 17 '22 14:10

Longfish