我有两个包含多列的表。现在我想使用like运算符合并它们。我的代码如下
proc sql;
select a.*,b.*
from tera.SFTP as a,home.SFTP_1 as b
where a.zip_c like ('%b.zip_extract%')
;
quit;
我收到分页符消息,但没有返回任何结果。a.zip_c 的列类型为 Char,长度为 50,而 b.zip_extract 的列类型为长度为 6 的 Char。
问题是您匹配的是字符串 b.zip_extract,而不是列。
尝试:
select a.*,b.*
from tera.SFTP as a,home.SFTP_1 as b
where a.zip_c like '%' || b.zip_extract || '%'
合并like
不是一个好主意。它不使用索引,也没有使用很多其他方式获得的优化。但是,有时它是必要的。
但是,在 SAS 中,我会以不同的方式执行此操作 [以及在大多数其他 SQL 中...]
proc sql;
select a.*,b.*
from tera.SFTP as a,home.SFTP_1 as b
where find(a.zip_c,b.zip_extract)
;
quit;
这完成了与 LIKE 相同的事情,但更可能允许 SAS 使用索引和优化,并且写得更清楚。
要处理可能的列长度问题,请使用 TRIM:
data have_a;
length zip_c $50;
input @1 zip_c $50.;
datalines;
12345 99999
67890 99999
88001 99999
37013 99999
60037 99999
;;;;
run;
data have_b;
length zip_extract $7;
input zip_extract $;
datalines;
12345
37013
99998
;;;;
run;
proc sql;
title "No Rows!";
select a.*,b.*
from have_a as a,have_b as b
where find(a.zip_c,b.zip_extract)
;
quit;
proc sql;
title "Rows!";
select a.*,b.*
from have_a as a,have_b as b
where find(a.zip_c,trim(b.zip_extract))
;
quit;