假设我有这三个表:
data Con1;
input ID Age;
cards;
11 22
23 32
32 3
14 73
;
run;
data Con2;
input ID Age;
cards;
11 22
23 32
32 3
14 73
15 11
7 15
;
run;
data Con3;
input ID Height;
cards;
11 150
23 180
32 181
14 175
15 149
7 159
;
run;
现在我想在 Con2
和 Con3
之间进行左连接,但仅限于 Con1
中没有的行。
proc sql;
create table works as
select *
from Con2 where ID not in (select ID from Con1);
run;
proc sql;
create table want as
select a.*, b.Height from works as a
left join Con3 as b
on a.ID=b.ID;
run;
这工作得很好,但我不喜欢这样的事实:我需要两个步骤。
所以我的目标是只需一步就可以实现它,看起来应该像这样:
proc sql;
create table want_fails as
select
a.*,
b.Height
from Con2 (where=(ID not in (select ID from Con1))) as a
left join Con3 as b
on a.ID=b.ID;
run;
我只是不明白这是如何解决错误的,而另一方面,以下语句运行得很好:
proc sql;
create table want_works as
select
a.*,
b.Height
from Con2 (where=(ID not in (11 23 32 14))) as a
left join Con3 as b
on a.ID=b.ID;
run;
这是错误:
错误 22-322:语法错误,需要以下之一:带引号的字符串、数字常量、日期时间常量、
缺失值,-.
错误 76-322:语法错误,语句将被忽略。错误:解析 WHERE 子句时出现语法错误。错误:WHERE 选项的值无效。错误:由于上述错误或警告,未处理文件 WORK.CON2 的某些选项。
那么你能告诉我我忽略了什么吗?
我只有另一个使用创建 View
的解决方案,但我更喜欢@user2877959的解决方案。
proc sql;
create view test as
select *
from Con2 where ID not in (select ID from Con1);
create table want_works2 as
select
a.*,b.Height from test as a
left join Con3 as b
on a.ID=b.ID;
最佳答案
where=
数据集选项中不能有子查询。
这不是你想要的吗?
proc sql;
create table want_works as
select a.*,
b.Height
from Con2 as a
left join Con3 as b
on a.ID=b.ID
where a.ID not in (select ID from Con1);
run;
关于sql - SAS:左连接适用于定义的数字,但不适用于选定的数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45754107/