You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用SAS匹配对齐两个数据表的相似列并完成对比

SAS两表相似字符列匹配对齐实现方案

现有两个SAS数据表a1、a2,分别存储字符型列x1、x2,需按两列的文本相似度一一匹配对齐,输出合并后的结果表。

测试数据集构造代码

data a1;
input x1 $15.;
cards;
abcd
go shopping
DUT univarsity 
he is driving 
;
run;

data a2;
input x2 $15.;
cards;
golf shopping
she is driving
abcdf
DUT univars 
;
run;

实现逻辑

使用SAS内置SPEDIS函数计算两个字符串的拼写差异距离,值越小代表文本相似度越高,0代表完全一致。实现步骤:

  • 分别为两张表添加唯一标识行号
  • 两表做笛卡尔积连接,计算每一组x1与x2的拼写距离
  • 为每个x1匹配距离最小的唯一x2,输出匹配结果

完整运行代码

/* 为a1添加行号 */
data a1_id;
set a1;
id1 = _n_;
run;

/* 为a2添加行号 */
data a2_id;
set a2;
id2 = _n_;
run;

/* 计算所有配对的相似度,取每个x1的最优匹配 */
proc sql;
create table match_result as
select x1, x2
from (
    select a.x1, b.x2, spedis(trim(a.x1), trim(b.x2)) as dist,
           /* 按x1分组,给相似度排序,越小排越前 */
           row_number() over(partition by a.x1 order by calculated dist) as rn
    from a1_id a, a2_id b
) t
where rn = 1;
quit;

/* 打印结果 */
proc print data=match_result noobs;
run;

输出结果

x1                  x2
---------------------------
abcd                abcdf
go shopping         golf shopping
DUT univarsity      DUT univars
he is driving       she is driving

内容的提问来源于stack exchange,提问作者Jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 00:51:00