如何让 Stata 在每次循环迭代中等待用户输入?
How to make Stata wait for user input in each loop iteration?
我遇到用户需要手动 review/edit 数据的情况。我想一次向他们展示大块数据。我正在使用循环执行此操作。我的问题是让循环等待用户说她已经完成了一个块,然后再移动到下一个块。
我尝试了 set more on
、pause on
和 _request
,但没有成功。这是我使用一些假数据设置的示例:
clear
set obs 100
gen id = int( 5*uniform() )
gen var1 = uniform()
levelsof id, local(ids)
foreach id of local ids {
edit if id == `id'
/*
pause,
wait for user to hit any key in
the Command Window before moving
to the next iteration
*/
}
根据 Nick Cox 的评论,这是我的解决方案。
clear
set obs 100
gen id = int( 5*uniform() )
gen var1 = uniform()
levelsof id, local(ids)
foreach id of local ids {
edit if id == `id'
* user must type _next_ in Command Window for next iteration
local cmd 0
while ("`cmd'" != "next") {
display _request(cmd)
local cmd "$cmd"
/* here's the trick: the edits (replace var1 = bla in bla)
are captured by _request(). I need to send the replace
command back to stata */
if ("`cmd'" != "next") {
`cmd'
}
}
}
我遇到用户需要手动 review/edit 数据的情况。我想一次向他们展示大块数据。我正在使用循环执行此操作。我的问题是让循环等待用户说她已经完成了一个块,然后再移动到下一个块。
我尝试了 set more on
、pause on
和 _request
,但没有成功。这是我使用一些假数据设置的示例:
clear
set obs 100
gen id = int( 5*uniform() )
gen var1 = uniform()
levelsof id, local(ids)
foreach id of local ids {
edit if id == `id'
/*
pause,
wait for user to hit any key in
the Command Window before moving
to the next iteration
*/
}
根据 Nick Cox 的评论,这是我的解决方案。
clear
set obs 100
gen id = int( 5*uniform() )
gen var1 = uniform()
levelsof id, local(ids)
foreach id of local ids {
edit if id == `id'
* user must type _next_ in Command Window for next iteration
local cmd 0
while ("`cmd'" != "next") {
display _request(cmd)
local cmd "$cmd"
/* here's the trick: the edits (replace var1 = bla in bla)
are captured by _request(). I need to send the replace
command back to stata */
if ("`cmd'" != "next") {
`cmd'
}
}
}