【问题标题】:SAS: Fuzzy JoinsSAS:模糊连接
【发布时间】:2022-01-10 07:55:56
【问题描述】:

我在 SAS 中运行以下 SQL 查询:

proc sql;
create table my_table as
select a.*, b.* 
from table_a a
inner join table_b b
on (a.date_1 between b.date_2 and b.date_3 and a.id1 = b.id1)
or a.id2 = b.id2;
quit;

我的问题:我正在尝试将“a.id1 = b.id1”替换为“a.id1 FUZZY EQUAL a.id1”(https://www.ibm.com/docs/en/psfa/7.2.1?topic=functions-fuzzy-string-search),并将其设为“显式直通”(https://www.lexjansen.com/mwsug/2013/RF/MWSUG-2013-RF02.pdf):

 proc sql;
connect to netezza(server = &abc database = &abc user =&abc password = &abc  bunkload = yes);
    create table my_table as
    select a.*, b.* 
    from table_a a
    inner join table_b b
    on (a.date_1 between b.date_2 and b.date_3 where le_dst(a.id1, b.id1) = 1 )
    or a.id2 = b.id2;
    quit;

但我是 SAS 新手,不知道如何正确执行此操作(表格在 netezza 上)。

有人可以告诉我如何做到这一点吗?是否还有其他常见的“模糊连接”函数非常适合此类问题?

谢谢!

注意1:表格如下所示:

> table_a

    id1 id2     date_1
1 123 A  11 2010-01-31
2 123BB  12 2010-01-31
3  12 5  14 2015-01-31
4 12--5  13 2018-01-31

> table_b

     id1 id2     date_2     date_3
1   0123 111 2009-01-31 2011-01-31
2   1233 112 2010-01-31 2010-01-31
3 125  .  14 2010-01-31 2020-01-31
4   125_ 113 2010-01-31 2020-01-31

注意 2: 用于为此示例创建这些表的 R 代码(在我的原始问题中,日期出现在 R 中的“因子”变量类型中):

table_a = data.frame(id1 = c("123 A", "123BB", "12 5", "12--5"), id2 = c("11", "12", "14", "13"),
date_1 = c("2010-01-31","2010-01-31", "2015-01-31", "2018-01-31" ))

table_a$id1 = as.factor(table_a$id1)
table_a$id2 = as.factor(table_a$id2)
table_a$date_1 = as.factor(table_a$date_1)

table_b = data.frame(id1 = c("0123", "1233", "125  .", "125_"), id2 = c("111", "112", "14", "113"),
date_2 = c("2009-01-31","2010-01-31", "2010-01-31", "2010-01-31" ),
date_3 = c("2011-01-31","2010-01-31", "2020-01-31", "2020-01-31" ))


table_b$id1 = as.factor(table_b$id1)
table_b$id2 = as.factor(table_b$id2)
table_b$date_2 = as.factor(table_b$date_2)
table_b$date_3 = as.factor(table_b$date_3)

【问题讨论】:

  • 您有在 Netezza 中工作的 SQL 代码吗?为什么不在 SAS 代码中使用 from connection to 在远程数据库中运行该 SQL?
  • @汤姆:谢谢你的回复!我使用的原始代码在 netezza 中成功运行(它很长,所以我没有在这里包含它)。谢谢!

标签: sql r sas data-manipulation netezza


【解决方案1】:

SAS® 9.4 SQL Procedure User’s Guide, Fourth Edition 文档的第 258 页显示了两种形式的 CONNECT 语句语法:

  • 连接到 dbms-name
    )>
    )>;

  • 连接使用 libref ;

用你的实际值替换斜体,之间的项目是可选的。

【讨论】:

    【解决方案2】:

    将 SQL 推送到远程数据库中。

    proc sql;
      connect to netezza .... ;
      create table sastable as 
        select * from connection to netezza
          (
    select a.*, b.* 
      from table_a a
      inner join table_b b
        on (a.date_1 between b.date_2 and b.date_3)
          and (le_dst(a.id1, b.id1) = 1 or a.id2 = b.id2)
         )
      ;
    quit;
    

    【讨论】:

    • @Tom:非常感谢您的回答!澄清一下,“连接到 netezza”需要这样定义吗?
    • 连接到 netezza(server=&NZServer 数据库=&NZdb user=&NZUser 密码=&NZPass BULKUNLOAD=YES);
    • 我认为其余的代码应该可以工作?
    • 是的。使用适用于您的数据库的凭据填写 CONNECT 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2017-01-31
    • 2020-07-14
    • 2019-05-24
    • 2022-01-10
    • 2020-03-22
    • 2021-02-01
    相关资源
    最近更新 更多