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

如何用SAS宏实现对指定变量列表执行Proc SQL查询?

实现方法

下面是满足需求的SAS宏,传入空格分隔的变量列表即可自动生成对应的Proc SQL查询:

%macro check_missing(col_list);
proc sql;
    select &col_list.
    from dataset
    where 
        %let i=1;
        %let col=%scan(&col_list., &i.);
        %do %while(&col. ne );
            &col. = .
            %let i=%eval(&i.+1);
            %let col=%scan(&col_list., &i.);
            %if &col. ne %then OR;
        %end;
    ;
quit;
%mend check_missing;

使用示例

调用宏时直接传入目标变量列表(空格分隔):

%check_missing(col1 col2 col3);

这会生成你提供的示例代码,自动完成WHERE子句中多个变量缺失值的OR连接。

扩展说明

如果需要兼容字符型变量(缺失值为''),可以修改宏里的条件判断部分,增加类型区分参数:

%macro check_missing(col_list, type=numeric);
proc sql;
    select &col_list.
    from dataset
    where 
        %let i=1;
        %let col=%scan(&col_list., &i.);
        %do %while(&col. ne );
            %if &type.=numeric %then &col. = .;
            %else &col. = '';
            %let i=%eval(&i.+1);
            %let col=%scan(&col_list., &i.);
            %if &col. ne %then OR;
        %end;
    ;
quit;
%mend check_missing;

调用字符型变量时:

%check_missing(char_col1 char_col2, type=character);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 11:50:18