SAS:选择ID在另一个表中的行

时间:2014-03-12 09:25:05

标签: sas

我有两个表,都有一个ID列。我想选择一个表中具有第二个表中的ID的行。

我会这样说tbl1 [tbl $ ID%in%tbl2 $ ID,],但我还没有办法将其翻译成SAS。

3 个答案:

答案 0 :(得分:1)

试试这个:

PROC SQL;
CREATE TABLE result AS
SELECT t2.*
FROM table1 AS t1, table2 AS t2
WHERE t1.id = t2.id
;
QUIT;

答案 1 :(得分:1)

这是对Hong Ooi方法的扩展,以及Jon Clements建议的更正。我发现使用数据步骤比使用SQL更快。它为您提供了更多输出数据的选项。例如,此解决方案创建一个名为" match_error"的表。它包含table1中表2中不包含的所有ID。

proc sort data=table1;
  by id;
run;

proc sort data=table2;
  by id;
run;

data result match_error;
  merge table1 (in=in_T1) table2 (in=in_T2 keep=id);
  by id;
  if in_T1 and in_T2 then output result;
  if in_T1 and not in_T2 then output match_error;
run;

答案 2 :(得分:-1)

data out;
  merge table1 (in=t1) table2 (in=t2);
  by id;
  if t1 and not t2;
run;
相关问题