gpt4 book ai didi

sas - 为什么 `%str(%inner_macro())` 中的 %str 不起作用?

转载 作者:行者123 更新时间:2023-12-04 14:12:29 26 4
gpt4 key购买 nike

%str 应该将字符串作为一个参数传递给 sas 宏,即使它包含逗号,但如果 %str 的参数本身是宏的结果,那显然不起作用?我得到 ERROR: More positional parameters found than defined.

例子

这就是错误的意思

    15         %macro outer_macro(left, right);
16 %put NOTE: outer_macro: left is &left;
17 %put NOTE: outer_macro: right is &right;
18 %mend;
19 %outer_macro(left, right);
NOTE: outer_macro: left is left
NOTE: outer_macro: right is right
20 %outer_macro(left, midle, right);
ERROR: More positional parameters found than defined.

这就是 %str 在正常情况下的解决方法。

    21         %outer_macro(left, %str(midle, right));
NOTE: outer_macro: left is left
NOTE: outer_macro: right is midle, right

可以用内部宏构造宏的参数

    23         %macro blank_macro(left, right);
24 %put NOTE: blank_macro: left is &left;
25 %put NOTE: blank_macro: right is &right;
26 &left. &right.
27 %mend;
28 %outer_macro(links, %blank_macro(midden, rechts));
NOTE: blank_macro: left is midden
NOTE: blank_macro: right is rechts
NOTE: outer_macro: left is links
NOTE: outer_macro: right is midden rechts

但是如果内部宏插入一个逗号,你会得到原来的错误

    30         %macro comma_macro(left, right);
31 %put NOTE: comma_macro: left is &left;
32 %put NOTE: comma_macro: right is &right;
33 &left., &right.
34 %mend;
35 %outer_macro(left, %comma_macro(midle, right));
NOTE: comma_macro: left is midle
NOTE: comma_macro: right is right
ERROR: More positional parameters found than defined.

我希望 %str 能够解决这个问题,但事实并非如此。为什么?

    36         %outer_macro(left, %str(%comma_macro(midle, right)));
NOTE: comma_macro: left is midle, right
NOTE: comma_macro: right is
ERROR: More positional parameters found than defined.

上下文

供您引用:我需要这个,因为我写了一个宏来列出一些我应该从数据库查询的字段,我需要通过另一个宏将我的 sql 传递到数据库。

%run_SQL(select key, %list_fields(some_arguments), from something);

最佳答案

您遇到的问题是 %str 在宏编译 期间屏蔽字符,而不是在宏执行 期间屏蔽字符。因此,宏调用会正确编译,执行 %comma_macro,但是一旦执行,值就不再被引用(因为 %str 已经完成了它的工作)。它不会第二次 掩盖这些值。

您的 %comma_macro 调用也是错误的,这进一步证明了这一点。它屏蔽了 ,,然后导致 %comma_macro 认为 midle,right 是第一个参数(与您使用 %str 在宏括号内)。

请注意,SAS 在 the %str documentation 中特别提到这种做法是危险的:

Do not use %STR to enclose other macro functions or macro invocations that have a list of parameter values. Because %STR masks parentheses without a match, the macro processor does not recognize the arguments of a function or the parameter values of a macro invocation.

%quote%str 相同,不同之处在于 masks characters during resolution :

%QUOTE and %NRQUOTE mask the same items as %STR and %NRSTR, respectively. However, %STR and %NRSTR mask constant text instead of a resolved value. And, %STR and %NRSTR work when a macro compiles, while %QUOTE and %NRQUOTE work when a macro executes.

在您的示例中,将 %str 替换为 %quote 会非常有效。我会争论%bquote然而,它可能是首选,因为它为不匹配的引号提供了额外的保护。我一直认为 %bquote 中的 b 代表 better,但从未见过使用 %quote 在上面。

The %BQUOTE and %NRBQUOTE functions mask a character string or resolved value of a text expression during execution of a macro or macro language statement.

这听起来像是你在对我做的。您不需要使用 %NRBQUOTE,除非 & % 字符可能在结果中。

宏引用时间线表:

Quoting Type  | Occurs At       | Mask details
%str | Compilation | Unmatched Quotations/Parens with %
%nrstr | Compilation | Same as %str plus & %
%quote | Execution | Same as %str
%nrquote | Execution | Same as %quote plus & %
%bquote | Execution | Unmatched quotes/parens automatically
%nrbquote | Execution | Same as %bquote plus & %
%superq | Execution | Only variable and without &, same list as %nbrquote
(continuned) | Does not attempt to resolve anything inside of variable

关于sas - 为什么 `%str(%inner_macro())` 中的 %str 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com