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

如何在SAS Enterprise Guide中删除SAS表特定后缀的列?

在SAS Enterprise Guide中删除指定后缀的列

以下两种代码方法可实现删除表中所有以_DT、_ID、_CP结尾的列(仅当这些列存在时执行操作):

方法一:直接修改原表结构

该方法直接修改原表,删除符合条件的列,无需生成新表。

/* 1. 查询系统字典表,获取需要删除的列名 */
proc sql noprint;
    select name into :drop_list separated by ' '
    from dictionary.columns
    where libname = 'WORK' /* 替换为你的表所在库名,需大写 */
      and memname = 'MYTABLE' /* 替换为你的表名,需大写 */
      and (name like '%_DT' or name like '%_ID' or name like '%_CP');
quit;

/* 2. 仅当查到需要删除的列时,执行删除操作 */
%if &sqlobs > 0 %then %do;
    proc sql;
        alter table WORK.MYTABLE
        drop &drop_list;
    quit;
%end;

说明:dictionary.columns是SAS内置的系统字典表,存储了所有SAS表的列信息;宏变量&sqlobs记录SQL查询的返回行数,确保仅存在目标列时才执行删除。

方法二:生成新表保留所需列

若不想直接修改原表结构,可通过生成新表保留不需要删除的列,再替换原表。

/* 1. 查询系统字典表,获取需要保留的列名 */
proc sql noprint;
    select name into :keep_list separated by ' '
    from dictionary.columns
    where libname = 'WORK'
      and memname = 'MYTABLE'
      and not (name like '%_DT' or name like '%_ID' or name like '%_CP');
quit;

/* 2. 创建仅保留目标列的新表 */
data WORK.MYTABLE_NEW;
    set WORK.MYTABLE;
    keep &keep_list;
run;

/* 3. 替换原表(可选操作) */
proc datasets lib=WORK nolist;
    delete MYTABLE;
    change MYTABLE_NEW=MYTABLE;
quit;

在SAS Enterprise Guide中的操作步骤

  1. 打开SAS Enterprise Guide,新建一个SAS程序节点;
  2. 将上述任意一段代码粘贴到编辑区,修改代码中的库名(WORK)和表名(MYTABLE)为你实际的库名和表名;
  3. 点击工具栏的运行按钮,执行代码即可完成操作。

内容的提问来源于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 05:50:21