0

I am using %SYMEXIST to check if a macro variable exists and then continue or skip based on the result. It sounds so simple but SAS is throwing errors for all the approaches I have tried so far.

&num_tables is a macro created from a dataset based on certain conditions.

proc sql noprint;
select distinct data_name into :num_tables separated by ' '
from TP_data
where trim(upcase(Data_Name)) in 
  (select distinct(trim(upcase(Data_Name))) from Check_table
   where COALESCE(Num_Attri_DR,0)-COALESCE(Num_Attri_Data,0) = 0
   and Name_Missing_Column eq ' ' and Var_Name eq ' ');
quit;

If this macro var is not resolved or not created (no rows selected from the dataset), I would like to skip. When I used,

 %if %symexist(num_tables) %then %do;

SAS gives an error with the message "MACRO variable name X must start with a letter or underscore". So I tried removing leading spaces using all of the following approaches:

 %let num_tables = &num_tables; /* approach 1 */
 %let num_tables = %sysfunc(trim(&num_tables)) /* approach 2 */
 %let num_tables = %trim(&num_tables) /* approach 3 */

But none of these worked. I still get the error "MACRO variable name X must start with a letter or underscore"

rom
  • 81
  • 4
  • 10
  • Where does "X" come from"? Are you doing actually `%if %symexist(&num_tables)` perhaps? – Joe Jul 01 '14 at 18:57
  • I forgot to replace the X with num_tables, just wanted to make it generic. Also, I am not using an & before num_tables. So my code was %if %symexist(num_tables) %then %do; – rom Jul 01 '14 at 19:04
  • That's not consistent with the error you're giving, though; `%if %symexist(num_tables)` is legal syntax in any SAS session. None of the things you're doing in the 'approaches' makes sense with this - they all modify the contents of num_tables, not the name itself. – Joe Jul 01 '14 at 19:06
  • Thanks @Joe, it worked finally - the error just went away this time!!. But nothing below the "%if symexist...%end" is getting executed. I checked the value by using a put statement and it returned 0 correctly. %put %symexist(num_tables); %if %symexist(num_tables) %then %do; Any thoughts on this? – rom Jul 01 '14 at 19:22
  • 0 is false, so nothing after the if statement would be executed if it were 0. – Joe Jul 01 '14 at 19:27
  • I mean the codes after - %if %symexist...%end; Anyway, I found the reason. – rom Jul 01 '14 at 19:29

1 Answers1

0

Likely you are prefacing the num_tables in symexist with a &. This is the correct way to implement %SYMEXIST in the manner you ask. Note that the argument to %symexist is not &num_Tables but num_tables (the actual name of the macro variable). &num_tables would resolve to whatever its contents would be if you used it with the &.

%macro testshort(char=);
proc sql noprint;
select distinct name into :num_tables separated by ' '
from sashelp.class
where substr(name,1,1)="&char.";   
quit;
%if %symexist(num_tables) %then %do;
%put Tables: &num_tables;
%end;
%mend testshort;

%testshort(char=A);
%testshort(char=B);
%testshort(char=Z);
Joe
  • 62,789
  • 6
  • 49
  • 67