Why does the %str in `%str(%inner_macro())` not work?

余生颓废 提交于 2019-12-06 03:54:08

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

The problem you have is that %str masks characters during macro compilation, not during macro execution. So, the macro call compiles appropriately, executes %comma_macro, but then once that's executed, the values are no longer quoted (as %str has already done its thing). It won't mask the values a second time.

That's evidenced further by the fact that your %comma_macro call is also wrong. It masks the ,, which then leads to %comma_macro thinking midle,right is the first argument (identically to if you'd used the %str inside the macro parentheses).

Note that SAS specifically mentions this practice as dangerous in 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 is identical to %str, except it 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.

In your example, replacing %str with %quote would work perfectly well. I would argue %bquote is probably the preferred choice, however, as it provides additional protection for unmatched quotation marks. I've always assumed the b in %bquote stood for better, and never seen a reason to use %quote over it.

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.

That sounds like what you're doing to me. You don't need to use %NRBQUOTE, unless & % characters might be in the result.

The table of macro quoting timelines:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!