给dofile加个开关
Q:有时我只想运行长长的 do 文档中的一部分代码,怎么搞?
A: do-switches 模式,
i.e. 在 do 文档开头部分设置几个开关 (取值 0 或 1),进而通过「打开」或「关闭」这些开关来控制哪些代码被执行。
具体来说:
如果您想运行代码的某个部分,只需将该部分对应的 local 设置为 1(否则设置为 0 或其他值)。然后,用条件块将要打开或关闭的代码段括起来。
如果你只处理 B 部分,那么你可以将除 B 之外的所有开关设置为 0,然后运行整个 do-file,而无需手动选择其中的一部分。
思路
*-这三行是用来定义「开关」的
local A 0 // 第 1 个开关
local B 0 // 第 2 个开关
local merge 0 // 第 3 个开关
*-Note: 以下是代码主体
if `A' == 1 {
* Import and save A
}
if `B' == 1 {
* Import and save B
}
if `merge' == 1 {
* Merge A with B and save
}
实例展示
在这个简单的例子中,只选择处理导入的 A ,将其设置为 1,B 和 marge 开关设置为 0,然后运行全部的 do-file。
sysuse "nlsw88.dta"
global y "wage"
global x "hours tenure married collgrad"
local A 1
local B 0
local merge 0
if `A' == 1 {
reg $y $x
}
if `B' == 1 {
reg $y $x i.race i.industry
}
if `merge' == 1 {
reg $y $x
reg $y $x i.race i.industry
}

浙公网安备 33010602011771号