生成一个新的宏前缀词的旧宏
Generate a new macro prefixing words of an old macro
定义宏try
:
local try "a b c"
这将生成一个值为 a b c
的宏。
现在我想要一个新的宏 try2
,它采用值 da db dc
。也就是我想给旧宏的每个元素加上相同的前缀,定义为新的宏
据我所知,Stata官方没有专门支持的功能。有关附近的功能,请参阅 help macrolists
。
旧包 listutil
(SSC) 包含各种命令,prelist
是相关的。我写了那篇文章,所以并不是对其他人的工作持否定态度,这让我建议只应用一般技术。
local try "a b c"
local copy `try'
gettoken first copy : copy
while ("`first'" != "") {
local try2 `try2' d`first'
gettoken first copy : copy
}
di "try is {col 12} `try'"
di "try2 is{col 12} `try2'"
您也可以使用 Mata 来做到这一点。
local try "a b c"
mata: st_local("try2", invtokens("d" :+ tokens(st_local("try"))))
assert "`try2'" == "da db dc"
换句话说,第二行就是干这个的,先解释最里面的函数:
st_local("try")
:访问局部变量中的内容。这应该评估为 "a b c".
tokens("a b c")
:将字符串拆分为标记,例如tokens("a b c")
-> ("a", "b", "c")
.
"d" :+ ("a", "b", "c")
:在 Mata 中,您可以使用 +
连接字符串,这里 :+
按元素执行此操作,因此结果将是 ("da" , "db", 直流").
invtokens(("da", "db", dc"))
:将标记放回字符串中,即 invtokens(("da", "db", dc"))
-> "da db dc"
.
st_local("try2", "da db dc")
:相当于 local try2 "da db dc"
. 的 Mata
您可以找到更多关于 Mata 函数 st_local()
、tokens()
和 invtokens()
的信息,例如help mf_st_local
.
定义宏try
:
local try "a b c"
这将生成一个值为 a b c
的宏。
现在我想要一个新的宏 try2
,它采用值 da db dc
。也就是我想给旧宏的每个元素加上相同的前缀,定义为新的宏
据我所知,Stata官方没有专门支持的功能。有关附近的功能,请参阅 help macrolists
。
旧包 listutil
(SSC) 包含各种命令,prelist
是相关的。我写了那篇文章,所以并不是对其他人的工作持否定态度,这让我建议只应用一般技术。
local try "a b c"
local copy `try'
gettoken first copy : copy
while ("`first'" != "") {
local try2 `try2' d`first'
gettoken first copy : copy
}
di "try is {col 12} `try'"
di "try2 is{col 12} `try2'"
您也可以使用 Mata 来做到这一点。
local try "a b c"
mata: st_local("try2", invtokens("d" :+ tokens(st_local("try"))))
assert "`try2'" == "da db dc"
换句话说,第二行就是干这个的,先解释最里面的函数:
st_local("try")
:访问局部变量中的内容。这应该评估为 "a b c".tokens("a b c")
:将字符串拆分为标记,例如tokens("a b c")
->("a", "b", "c")
."d" :+ ("a", "b", "c")
:在 Mata 中,您可以使用+
连接字符串,这里:+
按元素执行此操作,因此结果将是 ("da" , "db", 直流").invtokens(("da", "db", dc"))
:将标记放回字符串中,即invtokens(("da", "db", dc"))
->"da db dc"
.st_local("try2", "da db dc")
:相当于local try2 "da db dc"
. 的 Mata
您可以找到更多关于 Mata 函数 st_local()
、tokens()
和 invtokens()
的信息,例如help mf_st_local
.