0

在我的 .do 文件中,我使用 .do 向用户询问他们想要运行哪些回归的问题display _request(macroname)。然后我解释他们的答案(yes或者no在大多数情况下)并定义另一个出现在回归代码中的宏。我这样做是因为有很多不同的选项来运行哪个回归,这使得选择运行哪个回归变得容易。

现在我想自动化一个过程,它循环遍历所有不同的选项,我认为在开始时添加一个额外的问题会很好。这将询问用户是否也不想费心回答问题,而是运行 64 种组合(其输出将保存到各种适当命名的不同 Word 文档中)。

所以我有需要运行的回归代码,但它需要在问五个问题后只运行一次,或者循环运行。

我可以将代码保存到宏中,然后指定何时运行代码吗?

编辑:我最好的解决方案是使用do命令运行一个单独的 .do 文件。如果我需要更频繁地执行此操作,是否可以在不运行单独的 .do 文件的情况下完成此操作?

********************** Regressions ***********************

* If you haven't yet installed estout on your machine, you need to do that.
 ssc install estout

pause Note that the regressions will be put in $DataOUT


capture window stopbox rusure "DO YOU WANT TO RUN EVERY POSSIBLE REGRESSION?" "This might take about 10 minutes."
if _rc == 0 {
    global cheat_yn = "yes"
}
if _rc == 1 {
    global cheat_yn = "no"
}


if "$cheat_yn" != "yes" {

/* Great! You're now at the end of the .do file! I'm going to ask you to type
in answers to some questions, to determine what variables you want to work
with. Just type in your answer in the command field, and press enter. */

* 1. Please enter a dependent variable out of searches, encouraged, employed or participates
display _request(dep)
* 2. Grant X female recipient interaction? Enter yes or no.
display _request(gXf_yn)
* 3. Grant X matric interaction? Enter yes or no.
display _request(gXmat)
* 4. Define grant independent variable to include 3 or 5 grant types? Enter 3 or 5.
display _request(n_3_5)
* 5. Dummy for grant or income? Enter dummy or income. (Note that the sample with dummy includes households with no grant income whereas the sample with the income variable only includes households with a positive grant income.)
display _request(grantd)
* 6. Separate out grant into different types or not? Enter yes or no.
display _request(separate)

do "$DataIN\running_regressions.do"

}


if "$cheat_yn" == "yes" {
pause off
foreach dependent in employed participates encouraged searches {
global dep = "`dependent'"
foreach f in no yes {
global gXf_yn = "`f'"
foreach d in income dummy {
global grantd = "`d'"
foreach s in no yes {
global separate = "`s'"
foreach n in 3 5 {
global n_3_5 = "`n'"
foreach m in yes no {
global gXmat = "`m'"
if ( "`dependent'" != "searches" & "`dependent'" != "encouraged" ) | "`f'" != "yes" | "`d'" != "dummy" | "`s'" != "yes" {
* The previous line removes a few regressions that don't run properly

do "$DataIN\running_regressions.do"

}
}
}
}
}
}
}

}
4

1 回答 1

0

使用宏注释掉脚本行有一个技巧:(不像写程序那么优雅……)

if answer = "yes" local star = ""
else local star = "*"

然后将'star'首先放在脚本行上:如果'star'为空,则运行该行,但如果'star'包含星号,则该行被视为注释:

`star' display "hi, there"

只有当“答案”是“是”时才会运行。

如果您可以构建您的脚本,以便可以注释掉循环的开始行和结束行,并且循环内的命令仍然可以正常工作,那么这可能会对您有所帮助。

于 2018-09-20T13:13:28.870 回答