简体   繁体   English

如何通过 SAS 中另一个表中的内容过滤表?

[英]How to filter a table by what is NOT in another table in SAS?

I appreciate the help with this:我很感激这方面的帮助:

I have a table A with 2 columns ID and var1.我有一个包含 2 列 ID 和 var1 的表 A。

ID  | var1
____________
1   |  4
2   |  5
3   |  2
4   |  3

I have another table B with other columns but only some of the rows:我有另一个表 B 有其他列,但只有一些行:

ID  | var2
____________
2   |  "Apple"
4   |  "Orange"

What I want is to be able to create a table of all rows in A for which the ID is NOT in table B我想要的是能够创建 A 中所有行的表,其中 ID 不在表 B 中

ID  | var1
____________
1   |  4
3   |  2

I could do a left join of A to B, filter using 'where var2 is null' and then drop that column, but that seems like an overly complicated way to deal with this.我可以将 A 左连接到 B,使用“var2 为 null”进行过滤,然后删除该列,但这似乎是一种过于复杂的处理方式。

Slight modification on one of Richard's solution above, SQL using not in instead.对 Richard 的上述解决方案之一进行了轻微修改,SQL 使用not in代替。

proc sql;
  create table want_way2 as
  select * from have
  where ID not in (select ID from filter)
; 
quit;

You can merge with IN= automatic flags, or SQL query with an existential predicate您可以与IN=自动标志或 SQL 查询与存在谓词合并

Example:例子:

data have;
input ID  var1; datalines;
1     4
2     5
3     2
4     3
;
data filter;
input ID  var2 $; datalines;
2     Apple
4     Orange
;

data want_way1;
  merge have filter (in=filter keep=ID);
  by ID;
  if not filter;
run;

proc sql;
  create table want_way2 as
  select * from have
  where not exists (select * from filter where filter.ID=have.ID)
; 

A third way could be a JOIN to an exception sub-select第三种方式可能是 JOIN 到异常子选择

proc sql; 
  create table want_way3 as
  select have.* from have
  join
  (select ID from have except select ID from filter) as keeper
  on have.ID = keeper.ID
  ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM