【问题标题】:Leading space error when using %SYMEXIST使用 %SYMEXIST 时出现前导空格错误
【发布时间】:2014-07-01 18:49:27
【问题描述】:

我正在使用 %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 给出错误消息“MACRO 变量名 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 必须以字母或下划线开头”

【问题讨论】:

  • “X”是从哪里来的?也许你真的在做%if %symexist(&num_tables)
  • 我忘了用 num_tables 替换 X,只是想让它通用。另外,我没有在 num_tables 之前使用 &。所以我的代码是 %if %symexist(num_tables) %then %do;
  • 不过,这与您给出的错误不一致; %if %symexist(num_tables) 是任何 SAS 会话中的合法语法。您在“方法”中所做的任何事情都没有意义 - 它们都修改了 num_tables 的内容,而不是名称本身。
  • 谢谢@Joe,终于成功了——这次错误就消失了!!。但是“%if symexist...%end”之下的任何内容都没有被执行。我使用 put 语句检查了该值,它正确返回了 0。 %put %symexist(num_tables); %if %symexist(num_tables) %then %do;对此有什么想法吗?
  • 0 为假,因此如果 if 语句为 0,则不会执行任何内容。

标签: sas sas-macro


【解决方案1】:

您可能在 symexist 中的 num_tables 前面带有 &。这是以您要求的方式实现 %SYMEXIST 的正确方法。请注意,%symexist 的参数不是&num_Tables,而是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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2020-06-14
    • 2013-06-30
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多