【问题标题】:How to Resolve Keyword Error with SAS Macro如何使用 SAS 宏解决关键字错误
【发布时间】:2017-03-20 20:43:14
【问题描述】:

我在 SAS 中运行这个 sql 宏。

%macro calc(table=,cut=,whereclause=);
proc sql;
&table
   select 
        &cut as type format = $40. length = 40
       ,dt
       ,count(prod_nbr) as stat
       ,sum(new) as new
       ,sum(old) as old
       ,sum(retired) as retired
       ,sum(replaced) as replaced
       ,sum(final) as final
       ,sum(redo) as redo

from work.product
where retail_flg = 1
&whereclause
group by 1,2;
quit;
%mend calc;

我在程序中调用了大约 60 次宏,它在我调用它时大约 80% 的时间都在工作。但是每隔一段时间它就会产生这个错误: ERROR: All positional parameters must precede keyword parameters

如果我以相同的顺序运行代码,错误总是显示在同一行。但是,如果我开始以不同的顺序运行调用,错误最终将出现在看似随机的调用宏的代码行上。下面是其中一个调用的示例(在创建计算表之后):

%calc(table = insert into calc, cut = 'Product', whereclause = and brand = 'JNJ' and Prod_type = 'N' and index(prod_nm, 'NEW') > 0);

我对这个错误特别困惑,因为我在宏中没有任何位置参数。我已经研究并解决了语法错误和其他常见问题,但无法解决错误。

【问题讨论】:

  • 你怎么叫它多次?通过其他宏?调用执行?
  • 我只是像上面那样运行多行代码。例如: %calc(table = insert into calc, cut = 'Product', whereclause = and brand = 'JNJ' ); %calc(table = insert into calc, cut = 'New Produce', whereclause = and brand = 'WMT' and Prod_type = 'Q');
  • 所以你是手动输入的?
  • @Tom 是的。我只使用 SAS 3 个月。这是我知道的唯一方法。

标签: macros sas params-keyword


【解决方案1】:

您很可能在某处有不平衡的引号。也许你有一些价值 WHERECLAUSE 有不平衡的报价。我会查看生成错误消息之前的值。这也可能是代码被截断的结果。例如,如果您将生成的代码写入文件,则某些较长的 WHERECLAUSE 值可能会被截断并导致引号不平衡。

在宏调用中添加一些额外的换行符可以更容易检查错误,因为较短的行更容易被人类扫描。

%calc
(table = insert into calc
,cut = 'Product'
,whereclause =
  and brand = 'JNJ' and Prod_type = 'N'
  and index(prod_nm, 'NEW') > 0
);

【讨论】:

  • 在我的故障排除中,我不时地看到一个关于截断的警告。为什么 SAS 会截断我的 WHERECLAUSE,我该如何防止它发生?
  • 这取决于您提交代码的方式。如果您使用的是 SAS Display Manager 编辑器,那么编辑器可以处理的代码行的长度会受到限制。
  • 我认为这是我的问题。我正在寻找解决此问题的方法。代码行太长有什么解决办法吗?
  • 只需在代码中添加一些换行符。只要你不把它们放在带引号的字符串或关键字的中间就没有关系。
  • 就是这样!一个非常简单的解决方案。几个小时以来,我一直在解决各种问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多