为什么 `%str(%inner_macro())` 中的 %str 不起作用? [英] Why does the %str in `%str(%inner_macro())` not work?
问题描述
%str 应该将字符串作为一个参数传递给 sas 宏,即使它包含逗号,但如果 %str 的参数本身是宏的结果,那显然不起作用?我得到 ERROR: More positional parameters found than defined.
%str should pass a string as one parameter to a sas macro, even if it contains commas, but that apparently does not work if the argument of %str is in it self the result of a macro? I get 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
在正常情况下的解决方法.
This is how %str
resolves it in normal situations.
21 %outer_macro(left, %str(midle, right));
NOTE: outer_macro: left is left
NOTE: outer_macro: right is midle, right
可以用内部宏构造宏的参数
It is possible to construct an argument of a macro with an inner macro
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
但是如果内部宏插入逗号,则会得到原始错误
But if the inner macro inserts a comma, you get the original error
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
能够解决这个问题,但事实并非如此.为什么?
I would expect %str
to resolve this, but it does not. Why?
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 传递给数据库.
Context
For your information: I need this because I wrote a macro to list a few fields I should query from a database and I need to pass my sql to the database via another macro.
%run_SQL(select key, %list_fields(some_arguments), from something);
推荐答案
引用 %COMMA_MACRO 的输出
Quote the output from %COMMA_MACRO
33 %macro comma_macro(left, right);
34 %put NOTE: comma_macro: left is &left;
35 %put NOTE: comma_macro: right is &right;
36 %nrbquote(&left., &right.)
37 %mend;
38 %macro outer_macro(left, right);
39 %put NOTE: outer_macro: left is &left;
40 %put NOTE: outer_macro: right is &right;
41 %mend;
42 %outer_macro(left, %comma_macro(midle, right));
NOTE: comma_macro: left is midle
NOTE: comma_macro: right is right
NOTE: outer_macro: left is left
NOTE: outer_macro: right is midle, right
这篇关于为什么 `%str(%inner_macro())` 中的 %str 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!