2014-07-01 55 views
0

我正在使用%SYMEXIST来检查宏变量是否存在,然后根据结果继续或跳过。这听起来很简单,但SAS迄今为止尝试的所有方法都出现错误。使用%SYMEXIST时导致空格错误

& num_tables是根据特定条件从数据集创建的宏。

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; 

如果这个宏var没有解析或没有创建(没有从数据集中选择行),我想跳过。当我用,

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

SAS给出错误消息“宏变量名X必须以字母开头或下划线”。所以我尝试使用以下所有方法删除前导空格:

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

但是这些都没有奏效。我仍然收到错误“MACRO变量名称X必须以字母或下划线开头”

+0

哪里“X”从何而来“?你实际做'如果%% symexist(num_tables)'也许? – Joe

+0

我忘了替换num_tables的X,只是想使其通用性。另外,我我的代码是%if%symexist(num_tables)%then%do; – rom

+0

这与您提供的错误不一致;'%if%symexist(num_tables)'是合法的语法在任何SAS会话中,您在'方法'中所做的任何事情都没有意义 - 它们都修改num_tables的内容,而不是名称本身。 – Joe

回答

0

可能您正在使用&在symexist中为num_tables做好准备。这是以您所要求的方式实现%SYMEXIST的正确方法。请注意,%symexist的参数不是&num_Tables,而是num_tables(宏变量的实际名称)。如果您将其与&一起使用,它将解析为其任何内容。

%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);