You can run non-Stata commands from within Stata. cf and its Windows-specific cousin .
Problem is, Stata doesn’t wait to see how your command turned out — it just keeps going.
Here’s where semaphores come in handy. I’ll spare background, because does a fine job. Applied to waiting, and to Stata, the idea is as follows:
- Create a file somewhere. This will serve as your semaphore
- Launches a non-Stata command via -shell-.
- -while- the file still exists, -pause- for a few seconds
- The non-Stata command somehow deletes the file you created — eg you’ve executed a batch file whose last command is to delete it.
- Once the file is gone, Stata exits the loop (moving on)
Here we go:
local semaphore "c:\myfolder\semaphore.xxx" // not vital to set a local, but cleaner
cap noi winexec "echo XXX > `semaphore'" // 'XXX' is unimportant; you just need to create the file
winexec "c:\myfolder\mybatchfile.bat" // do our non-Stata thing!
local checkflag=1 // whatever value you like, of course
while `checkflag'==1 {
cap confirm file "$semaphore" // is the file still there?
if _rc==0 {
pause 10000 // 10secs
}
else {
local checkflag==0 // let's break. -continue, break- would do just fine, too.
}
}
Now, I’ve tucked my non-Stata commands into a DOS batch file. It looks only slightly like this:
command number one
command number two
command number three
del c:\myfolder\semaphore.xxx
As long as my batch file executes correctly — that is, it has to make it to that fourth ‘del’ line — Stata will wait patiently and then resume. If the batch file fails, at least Stata will continue waiting patiently rather than proceeding when you don’t want it to.