使用宏变量解析多个 & 符号 [英] 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),我可以在我的开发和生产数据库之间无缝切换.
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屋!