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

SAS如何按指定条件为每个Account筛选保留唯一观测行

实现思路

不需要单独判断Account对应记录的数量,直接给所有记录按筛选规则设置优先级,分组取优先级最高的记录即可,逻辑更简洁,可完全匹配需求规则。

实现代码
/* 构造测试数据集,实际使用时替换为你自己的输入数据集即可 */
data have;
    input Account Model $ Class Value;
    datalines;
1 A 0 1.0
1 B 0 1.0
2 B 0 0.5
3 A 1 0.5
3 A 0 1.0
;
run;

/* 按规则给每条记录计算优先级,数值越高优先级越高 */
data temp;
    set have;
    if Model = 'A' and Class = 1 then priority = 3;
    else if Model = 'A' then priority = 2;
    else priority = 1;
run;

/* 按Account分组,组内按优先级降序排序 */
proc sort data=temp;
    by Account descending priority;
run;

/* 每个Account仅保留优先级最高的第一条记录 */
data want;
    set temp;
    by Account;
    if first.Account;
    drop priority;
run;
结果验证

运行后得到的want数据集和预期输出完全一致:

AccountModelClassValue
1A01.0
2B00.5
3A10.5

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 13:45:01