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

实现SAS一对多非常规合并:按grp匹配生成目标数据集

SAS数据集按组全匹配解决方案

需求:将数据集one中每个grp对应的行,与数据集two中同grp的所有行进行匹配,生成目标输出数据集Want。

方法一:Proc SQL 隐式交叉连接(推荐)

通过SQL隐式交叉连接结合grp相等条件,实现同组行的全匹配:

proc sql;
    create table want as
    select 
        one.accno,
        one.grp as agentgrp,
        two.inc,
        two.exc,
        two.desc
    from one, two
    where one.grp = two.grp
    order by accno, desc; -- 排序与示例输出一致,可选
quit;

逻辑说明:from one, two生成两个数据集的笛卡尔积,where one.grp = two.grp筛选出同组组合,最终每个one的行都会对应two中同grp的所有行。

方法二:Data Step 分组循环匹配

利用Data Step分组循环,逐个读取one的组并遍历对应two的组内所有行:

-- 先确保数据集按grp排序(若未排序需执行)
proc sort data=one; by grp; run;
proc sort data=two; by grp; run;

data want;
    do until (last.grp);
        set one;
        by grp;
        do until (last.grp_two);
            set two(rename=(grp=grp_two));
            by grp_two;
            agentgrp = grp;
            output;
        end;
    end;
run;

逻辑说明:外层循环处理one的每个grp组,内层循环读取two中同组(重命名grp为grp_two避免变量冲突)的所有行,每读取一行就输出组合结果,实现全匹配。

输出结果

两种方法均会生成与目标一致的数据集:

accno agentgrp inc exc desc
----- -------- --- --- ----
123   101      123 .   abc
123   101      .   123 def
123   101      .   .   ghi
234   101      123 .   abc
234   101      .   123 def
234   101      .   .   ghi

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:24:59