name
name

Reputation: 41

SAS check if column exist in Table

I want to make a macro That check if all the columns in col_to_check are in Table and I want exit SAS if one of these columns doesn't exit

I try this:

%let col_to_check = ID SEG AGE;

%MACRO check(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 check;

Upvotes: 0

Views: 1524

Answers (1)

Kermit
Kermit

Reputation: 3117

Use the vcolumn table of the sashelp library.

%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: What do you mean by "exit SAS"? You could easily replace the %goto by %ABORT

Upvotes: 1

Related Questions