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

在SAS Enterprise Guide中为TABLE1符合条件的数值列填充0补缺失值

SAS Enterprise Guide:填充TABLE1指定缺失值方案

问题背景

现有三张表结构如下:

TABLE 1(存在缺失值):

COL1 | COL2 | ...  | COLn
-----|------|------|-------
123  |      | ...  | xxx
     | AAA  | ...  | xxx
122  | BCC  | ...  | xxx
...  | ...  | ...  | xxx

TABLE 2:

COL1 | ...| COLn
-----|----|------
998  | ...| xxx
999  | ...| xxx
001  | ...| xxx
...  | ...| ...

TABLE 3:

COL8 | ...| COLn
-----|----|------
117  | ...| xxx
906  | ...| xxx
201  | ...| xxx
...  | ...| ...

需求

仅处理TABLE 1,对满足以下条件的列,将缺失值用0填充:

  • 该列是数值型
  • 该列存在于TABLE 2或TABLE 3中
  • 该列存在缺失值

实现代码

步骤1:自动识别符合条件的列

通过SAS字典表DICTIONARY.COLUMNS自动筛选目标列,避免手动维护列名:

/* 修改libname为表实际所在库,比如'SASUSER',表名需大写 */
%let lib = WORK;

/* 获取TABLE2和TABLE3的所有列名 */
proc sql noprint;
    select distinct name into :t2t3_cols separated by ' '
    from dictionary.columns
    where libname="&lib."
      and memname in ('TABLE2','TABLE3');
quit;

/* 获取TABLE1中数值型且在TABLE2/TABLE3中存在的列名 */
proc sql noprint;
    select name into :target_cols separated by ' '
    from dictionary.columns
    where libname="&lib."
      and memname='TABLE1'
      and type='num'
      and name in (&t2t3_cols.);
quit;

步骤2:填充缺失值

用DATA步遍历目标列,替换缺失值为0:

/* 生成填充后的新表TABLE1_FILLED */
data &lib..TABLE1_FILLED;
    set &lib..TABLE1;
    array target_cols[*] &target_cols.;
    do i = 1 to dim(target_cols);
        if missing(target_cols[i]) then target_cols[i] = 0;
    end;
    drop i;
run;

操作说明

在SAS Enterprise Guide中,将上述代码复制到程序节点中,修改%let lib = WORK;为表实际所在的库名(如SASUSER),运行后即可得到处理完成的TABLE1_FILLED表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 08:25:21