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

如何用SAS标记跨列连续重复单词及行中无间隔的'Add'?

解决方案

1. 标记跨列按顺序重复出现的单词

可以通过SAS数组结合循环实现,核心思路是遍历每一行的列,对比当前列与前一列的内容,若相同则标记该行存在重复序列。

示例代码:

data marked_duplicates;
    set your_dataset; /* 替换为你的数据集名 */
    array cols[*] col1-colN; /* 替换为实际需要检查的列名 */
    length duplicate_flag $1;
    duplicate_flag = 'N';
    
    do i = 2 to dim(cols);
        /* 跳过缺失值,仅对比非空内容 */
        if not missing(cols[i]) and not missing(cols[i-1]) and cols[i] = cols[i-1] then do;
            duplicate_flag = 'Y';
            leave; /* 找到一次就停止循环,提升效率 */
        end;
    end;
    drop i;
run;

代码说明:

  • 用array定义需要检查的列集合
  • 初始化duplicate_flag为'N',遍历列时发现连续重复则改为'Y'
  • 加入缺失值判断,避免误判缺失值为重复

2. 标记'Add'连续出现的行

针对"连续出现'Add'且中间无其他单词/缺失值"的需求,需要遍历数组时检查是否存在至少两个相邻的'Add'。

示例代码:

data marked_add;
    set your_dataset; /* 替换为你的数据集名 */
    array cols[*] col1-colN; /* 替换为实际需要检查的列名 */
    length add_consecutive_flag $1;
    add_consecutive_flag = 'N';
    
    do i = 2 to dim(cols);
        if cols[i] = 'Add' and cols[i-1] = 'Add' then do;
            add_consecutive_flag = 'Y';
            leave;
        end;
    end;
    drop i;
run;

代码说明:

  • 直接对比相邻两列是否均为'Add',满足则标记
  • 缺失值不等于'Add',因此天然排除了中间有缺失的情况

如果需要检查3次及以上的连续'Add'序列,可调整逻辑:

data marked_long_add;
    set your_dataset;
    array cols[*] col1-colN;
    length add_long_consecutive_flag $1;
    add_long_consecutive_flag = 'N';
    count = 0;
    
    do i = 1 to dim(cols);
        if cols[i] = 'Add' then do;
            count + 1;
            if count >= 2 then do; /* 修改数字可控制连续次数要求 */
                add_long_consecutive_flag = 'Y';
                leave;
            end;
        end;
        else count = 0; /* 遇到非Add则重置计数 */
    end;
    drop i count;
run;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 07:22:53