我使用%SYMEXIST检查宏变量是否存在,然后根据结果继续或跳过。这听起来很简单,但是对于我到目前为止尝试过的所有方法,SAS都会抛出错误。
&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;
如果此宏变量未解析或未创建(未从数据集中选择任何行),我想跳过。当我使用的时候
%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 */
但这些都不管用。我仍然收到错误“宏变量名X必须以字母或下划线开头”
发布于 2014-07-01 18:56:33
很可能您是在以&作为num_tables的前缀。这是按照您要求的方式实现%SYMEXIST的正确方法。注意,%symexist的参数不是&num_Tables
,而是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);
https://stackoverflow.com/questions/24517294
复制