SAS 检查 Table 中是否存在列

SAS check if column exist in Table

我想制作一个宏来检查 col_to_check 中的所有列是否都在 Table 中,如果这些列之一不存在,我想退出 SAS

我试试这个:

%let col_to_check = ID SEG AGE;

%MACRO 检查(table , col_to_check);

%local count;
%let count=0;

%DO i_=1 %TO %sysfunc(countw(&col_to_check.," "));  

        %LET col=%SCAN(&col_to_check.,&i_.," ");
        %if ( %varexist(&table.,&col.) = 1) %then endsas;
%END;

%MEND 检查;

使用 sashelp 库的 vcolumn table。

%macro check(lib, table, col_to_check);
    %let nb_col = %sysfunc(countw(&col_to_check., %quote( )));
    %let col_names = "%sysfunc(tranwrd(&col_to_check.,%str( )," "))";

    proc sql noprint;
        select count(distinct name) into :nb 
        from sashelp.vcolumn where upcase(name) in (&col_names.) 
           and upcase(libname)="&lib." 
           and upcase(memname)="&table.";
    quit;

    %if &nb. ^=&nb_col. %then
        %goto end_pg;
    %else
        %do;
            %put do stuff;
        %end;
%end_pg:
%mend;

%check(SASHELP, CLASS, SEX WEIGHT NAME AGE);

PS:“退出 SAS”是什么意思?您可以轻松地将 %goto 替换为 %ABORT