我想将noconstant
选项从包装程序传递到内部regress
调用。以下解决方案有效,但如果我想传递几个选项,它似乎特别笨拙且无法扩展。
webuse grunfeld, clear
capture program drop regress_wrapper
program define regress_wrapper
version 11.2
syntax varlist(min=2 numeric) [if] [in] ///
[, noconstant(string)]
tokenize `varlist'
local y `1'
macro shift
local x `*'
regress `y' `x', `noconstant'
end
regress_wrapper invest mvalue kstock
regress_wrapper invest mvalue kstock, noconstant(noconstant)
我认为更像下面的内容会起作用,但它没有通过noconstant
选项。
capture program drop regress_wrapper
program define regress_wrapper
version 11.2
syntax varlist(min=2 numeric) [if] [in] ///
[, noconstant]
tokenize `varlist'
local y `1'
macro shift
local x `*'
regress `y' `x', `noconstant'
end
regress_wrapper invest mvalue kstock
regress_wrapper invest mvalue kstock, noconstant
答案 0 :(得分:3)
第二个不起作用,因为本地宏最终被称为constant
,而不是noconstant
,如help syntax##optionally_off
所述。所以如果你替换它应该工作:
regress `y' `x', `noconstant'
by:
regress `y' `x', `constant'
如果您想要传递多个选项,则可以更轻松地使用help syntax##description_of_options
中解释的*
语法:
如果您还指定 *,任何剩余的选项被收集并一个接一个地放入 `选项。
e.g:
syntax varlist(min=2 numeric) [if] [in] ///
[, *]
...
regress `y' `x', `options'