@DavidPhillips2 wrote: How do I check if a table does exist and has no columns? SB: How do you encounter this situation? I mean, it's possible, but in all my years of SAS programming I've very rarely if ever encountered it. And I've never had to trap for it. The logic would be similar to something like this: data old; input ID SCORE1 SCORE2; cards; 24 100 97 28 98 87 60 100 97 65 100 98 70 99 97 40 97 99 190 100 99 196 100 100 210 98 85 ; run; %macro VarExist(ds, var); %local rc dsid result; %let dsid = %sysfunc(open(&ds)); %if %sysfunc(varnum(&dsid, &var)) > 0 %then %do; %let result = 1; %put NOTE: Var &var exists in &ds; %end; %else %do; %let result = 0; %put NOTE: Var &var not exists in &ds; data old; set old; score3=0; run; %end; %let rc = %sysfunc(close(&dsid)); %mend VarExist; %VarExist(old, score3); SB: Your macro is testing for the existence of a particularly named variable. Not "...check if a table does exist and has no columns". So which is it? What is your actual problem? https://communities.sas.com/t5/SAS-Programming/SAS-query/m-p/350433#M81457 When I run this: ERROR: You cannot open WORK.OLD.DATA for output access with member-level control because WORK.OLD.DATA is in use by you in resource environment DATASTEP (2). because the table already exists... Check: https://github.com/scottbass/SAS/blob/master/Macro/varexist.sas https://github.com/scottbass/SAS/blob/master/Macro/varlist.sas https://github.com/scottbass/SAS/blob/master/Macro/varlist2.sas Example calls: options mprint;
data empty;
stop;
run;
%put %varexist(sashelp.doesnotexist,foo);
%put %varexist(sashelp.class,sex);
%put %varexist(sashelp.class,foo);
%put %varexist(empty,foo);
%put %varexist(empty); * error, var is required ;
%put %varlist(sashelp.doesnotexist); * error, dataset must exist ;
%put %varlist(sashelp.class);
%put %varlist(empty);
%let empty=%eval(%varlist(sashelp.class) eq %str()); * test for empty string (or modify the macro) ;
%put &=empty;
%let empty=%eval(%varlist(empty) eq %str()); * test for empty string ;
%put &=empty;
%let varlist=;
%varlist2(sashelp.class (keep=name age sex));
%put &=varlist;
%let varlist=;
%varlist2(sashelp.class (keep=_numeric_));
%put &=varlist;
%let varlist=;
%varlist2(empty);
%put &=varlist; Read the macro header for other use cases...
... View more