Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: getting Stata to wait until a shell command is finished


From   ariley@stata.com (Alan Riley)
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: getting Stata to wait until a shell command is finished
Date   Thu, 20 Jun 2002 09:56:27 -0500

Gabriel Demombynes (gabriel@demog.berkeley.edu) wrote
> In the middle of my Stata program, I need to call a SAS batch routine (in
> Windows) and then have Stata wait until the SAS routine is finished, so that
> it can use part of the SAS output. However, when I use the shell command to
> call batch SAS, the SAS routine starts, and Stata doesn't wait. How can I
> get Stata to wait? I've tried using the sleep command, but then I have to
> guess how long SAS is going to take, and that time varies.
> 
> I think what I need to do is have Stata check for the existence of the SAS
> output file as a condition for continuing, and have it continue to sleep if
> the file doesn't exist. I'm sure Stata must have a logical command that
> checks for the existence of a file, but I can't find it. I did find the
> confirm command, but I can't figure out how to use its output. Please help!

If Gabriel is using Stata's -shell- command, and not Stata's
-winexec- command, then Stata should indeed wait for the SAS routine
to finish before continuing execution of Gabriel's do-file.

This can be tested with Notepad:

    . shell notepad

will result in Stata's Command window being hidden (to indicate
that it is not possible to do anything in Stata until control
returns to Stata) and the Notepad editor being launched.  Stata
will not regain control until Notepad is closed.

It is possible, however, for an application to launch itself in
such a way that control returns to Stata right away.  This may
be what SAS is doing.  In this case, Gabriel's thought of having
Stata wait and check for the existence of a file would be a
solution.

Stata can check for the existence of a file with the -confirm file-
command.  This command will return an error if the named file is
not found, so it should be used with the -capture- command prefixing it.
For example, a do-file might contain

    capture confirm file c:\wherever\output.txt
    while _rc {      /* file does not exist! */
        capture confirm file c:\wherever\output.txt
    }

which would result in Stata staying in the -while- loop until
the file c:\wherever\output.txt was created outside of Stata.

If this approach is taken, I would recommend putting a -sleep-
command within the -while- loop to prevent Stata from hogging
too much of the computer's resources by continuously checking
for the existence of the file.  It is probably sufficient to
check once every second, or perhaps even a longer interval
if the spawned task is known to take a very long time:

    capture confirm file c:\wherever\output.txt
    while _rc {      /* file does not exist! */
        sleep 1000   /* wait 1000 ms = 1 second before trying again */
        capture confirm file c:\wherever\output.txt
    }


--Alan
(ariley@stata.com)
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index