用宏变量解析多个&符号 [英] Resolving multiple ampersands with macro variables
问题描述
此代码适用于在本地运行的 SAS EG(隐藏敏感信息):
This code works in SAS EG run on local (hidden sensitive information):
*---- two values: DEV (ALIASDEV) and PROD (ALIASPROD);
%let my_environment = ALIASDEV;
%let ALIASPROD= (hidden_tns_prod);
%let ALIASDEV= (hidden_tns_dev);
libname mylib oracle user=username password='my_password' path="&&my_environment";
但是这段代码没有(使用 rsubmit;)
But this code doesn't (with rsubmit;)
rsubmit;
*---- two values: DEV (ALIASDEV) and PROD (ALIASPROD);
%let my_environment = ALIASDEV;
%let ALIASPROD= (hidden_tns_prod);
%let ALIASDEV= (hidden_tns_dev);
libname mylib oracle user=username password='my_password' path="&&my_environment";
endrsubmit;
这是错误信息:
错误:ORACLE 连接错误:ORA-12154:TNS:无法解析指定的连接标识符.错误:LIBNAME 语句中的错误.
ERROR: ORACLE connection error: ORA-12154: TNS:could not resolve the connect identifier specified. ERROR: Error in the LIBNAME statement.
我想要做的是拥有一个宏 (my_environment),我可以在我的 dev 和 prod 数据库之间无缝切换.
What I am trying to do is having a macro (my_environment) that I can switch to work seamlessly between my dev and prod databases.
谢谢
推荐答案
我不知道为什么它在本地工作,但是&符号需要三分之一才能正确解析.每当您将一个宏变量的值存储在另一个宏变量中时,都必须使用三个与号来检索它.
I don't know why it worked on the local, but the ampersands require a third to resolve properly. Any time you store the value of a macro variable in another macro variable, you must use three ampersands to retrieve it.
基本用例:
- 两个&符号允许您将包含其他宏变量的宏变量解析为名称的部分.IE,如果你有
&val_sept
和&val_oct
,你可以使用&&val_&mon
来检索它,假设%let mon=sept
. - 三个 & 符号允许您检索包含为另一个宏变量的 值 的宏变量.因此,如果您有
&sept
和&oct
,那么您将使用&&&mon.
来检索&sept
来自变量%let mon=sept
.
- Two ampersands allows you to resolve macro variables that contain other macro variables as part of the name. IE, if you have
&val_sept
and&val_oct
, you can use&&val_&mon
to retrieve it assuming%let mon=sept
. - Three ampersands allow you to retrieve a macro variable that is contained as a value of another macro variable. So if you have
&sept
and&oct
, then you would use&&&mon.
to retrieve&sept
from a variable%let mon=sept
.
这是因为多个&符号如何解析;SAS 多次通过,直到全部解决.
That's because of how multiple ampersands resolve; SAS makes multiple passes through until all are resolved.
在每一关中:
- 每对 & 符号解析为 1 个 & 符号,并被搁置一旁.
- 如果剩下一个 & 符号,它将与后面的文本一起解析为宏变量,并替换为存储在其中的值.
所以:
%let x=a;
%let a=b;
%let b=c;
%put &&x;
1: &&x
-> (&&)(x)
-> (&)(x)
-> &x
2: &x
-> a
1: &&x
-> (&&)(x)
-> (&)(x)
-> &x
2: &x
-> a
%put &&&x;
1: &&&x
-> (&&)(&x)
-> (&)(a)
-> &a
2:&a
-> b
1: &&&x
-> (&&)(&x)
-> (&)(a)
-> &a
2: &a
-> b
%put &&&&x;
1: &&&&x
-> (&&)(&&) (x)
-> (&)(&)(x)
-> &&x
2:&&x
-> (&&)(x)
-> (&)(x)
->&x
2: &x
-> a
1: &&&&x
-> (&&)(&&) (x)
-> (&)(&)(x)
-> &&x
2: &&x
-> (&&)(x)
-> (&)(x)
-> &x
2: &x
-> a
%put &&&&&x;
1: &&&&&x
-> (&&)(&&)(&x)
-> (&)(&)(a)
-> &&a
2:&&a
-> (&&)(a)
-> (&a
)3:&a
-> b
1: &&&&&x
-> (&&)(&&)(&x)
-> (&)(&)(a)
-> &&a
2: &&a
-> (&&)(a)
-> (&a
)
3: &a
-> b
%put &&&&&&x;
1: &&&&&&x
-> (&&)(&&)(&&) (x)
-> (&)(&)(&)(x)
-> &&&x
2:&&&a
-> (&&)(&x)
-> (&a
)3:&a
-> b
1: &&&&&&x
-> (&&)(&&)(&&) (x)
-> (&)(&)(&)(x)
-> &&&x
2: &&&a
-> (&&)(&x)
-> (&a
)
3: &a
-> b
四个 & 号对我来说是最有趣的,因为添加一个实际上会让你后退一步,有效地.
Four ampersands is the most interesting to me, since adding one actually takes you back a step, effectively.
有关详细信息,请参阅我在 sas MACRO && 上的回答.
See my answer on sas MACRO ampersand for more detail.
这篇关于用宏变量解析多个&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!