sas MACRO & 符号 [英] sas MACRO ampersand
问题描述
%let test = one;
%let one = two;
%put &test;
%put &&test;
%put &&&test;
%put &&&&test;
%put &&&&&test;
嗯.我完全被这些&符号打败了.我不明白为什么他们在宏变量之前需要这么多&符号.有没有什么技巧可以掌握&符号的用法?BTW,对应的五个结果是什么?
Well. I'm TOTALLY BEATEN by these ampersands. I don't understand why they need SO MANY ampersands before a macro variable. Is there any trick to master the usage of ampersand? BTW, what are the five results, correspondingly?
推荐答案
用一组 & 符号,你得到的东西很无聊;一后,奇数个与号导致解析两次,偶数个与号解析一次.因此,您使用 1 个和号来解析一次,使用 3 个和号来解析两次,除非您拥有拥有与号权利的公司的股票.
With a single set of ampersands, what you get is pretty boring; after one, odd number of ampersands leads to resolving twice, even number of ampersands resolves once. So you use 1 ampersand to resolve once and 3 ampersands to resolve twice, unless you have stock in the company that owns rights to the ampersand.
更有趣的是以下测试,它说明了为什么偶数个 & 符号具有价值:
More interesting is the following test, which shows why even numbers of ampersands have value:
%let test = one;
%let testtwo = one;
%let one = two;
%let two=three;
%put &test&one;
%put &&test&one;
%put &&&test&one;
%put &&&&test&one;
%put &&&&&test&one;
%put &&&&&&test&one;
基本上,每次通过,SAS 都会执行以下操作:
Basically, each pass through, SAS does the following:
- 将任何单个与号加文本解析为宏变量引用.
- 将任何对与号解析为一个与号.
那些是同时迭代完成的,直到所有的&符号都消失了,并且每个结果都保留用于下一次迭代,并且不会影响当前迭代.所以,&test&one
变成了 onetwo
因为 &test
-> one 和 &one
->二.剩余步骤:
Those are done simultaneously and iteratively until all ampersands are gone, and each result is kept for the next iteration and does not affect the current iteration. So, &test&one
becomes onetwo
because &test
-> one and &one
-> two. Steps for the remaining:
&&test&one
->&testtwo
->one
.&&|test|&one
.测试前的双&&
变成了&
,测试仍然存在,而&one
解析为two
.剩下的&testtwo
用于解析为one
的第二遍.&&&test&one
->&onetwo
-> 无法解析.&&|&test|&one
->&|one|two
-> DNR.&&&&test&one
->&&testtwo
->&testtwo
-> 一.&&|&&|test|&one
->&&|testtwo
->&testtwo
-> 一.两对都解析为一对,形成一对,然后再解析为一对,剩下&testtwo
来解析.&&&&&test&one
类似于三个与号的情况,但多了一对.&&&&&&test&one
解析为&&&testtwo
解析为&one
解析为两个.&&|&&|&&|test|&one
->&&|&testtwo
->&一
-> 二.奇数对意味着我们又得到了一组解析.
&&test&one
->&testtwo
->one
.&&|test|&one
. The double&&
before test becomes&
, test remains, and&one
resolves totwo
. That leaves&testtwo
for the second pass which resolves toone
.&&&test&one
->&onetwo
-> does not resolve.&&|&test|&one
->&|one|two
-> DNR.&&&&test&one
->&&testtwo
->&testtwo
-> one.&&|&&|test|&one
->&&|testtwo
->&testtwo
-> one. Two pairs each resolve down to one, making one pair, which then resolves to one, which leaves&testtwo
to resolve.&&&&&test&one
is similar to three ampersand case, but with one extra pair.&&&&&&test&one
resolves to&&&testtwo
resolves to&one
resolves to two.&&|&&|&&|test|&one
->&&|&testtwo
->&one
-> two. The odd number of pairs means we get one more set of resolves.
归根结底,你需要记住的是:
At the end of the day, what you need to remember:
- 1 & 符号解析宏变量一次,就是这样.
- 2 & 符号对于复合宏变量很有用,即前缀加上宏驱动的后缀 (
&&prefix&suffix
). - 3 &符号对于解析单个宏变量(
&&&var
->&var2
->var3
). - 6 & 符号对于解析两层深复合宏变量(即组合 2 和 3)很有用 ([
&prefix=var
,&suffix=2
>]&&&&&&prefix&suffix
->&&&var2
->&var3
code> ->4
).
- 1 ampersand resolves the macro variable once and that's it.
- 2 ampersands is useful for composite macro variables, ie, a prefix plus a macro-driven suffix (
&&prefix&suffix
). - 3 ampersands is useful for going two deep in resolving a single macro variable (
&&&var
->&var2
->var3
). - 6 ampersands is useful for resolving a two-deep composite macro variable (ie, combining 2 and 3) ([
&prefix=var
,&suffix=2
]&&&&&&prefix&suffix
->&&&var2
->&var3
->4
).
除此之外,4 个或更多(6 个除外)仅对特别复杂的组合有用;额外的级别将用于将解析延迟到特定时间.
Beyond that, 4 or more (other than 6) are useful only for particularly complex combinations; the extra levels would be used to delay resolution until particular times.
这篇关于sas MACRO & 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!