• Laura Whiting

Pause Stata with the Sleep Command

There are times when you want Stata to wait a while between running commands. For example, you are running a subroutine that outputs a file and you don’t want Stata to try and process the file before it is ready. You can use the sleep command for this purpose. This command will take an amount of time, in milliseconds, and pause for that length of time. If you know approximately how long you want Stata to wait for you can just give it the exact time in milliseconds. However, if you are unsure how long you will need Stata to wait, you can use sleep in a while{} loop.

How to Use:

Worked Example:

To make Stata sleep for 30 seconds you would use the following command:

Alternatively, if you are unsure how long you need Stata to wait, you can use a while{} loop. For example, if you are waiting for another file to be created before Stata can load it you could use the following loop:

This loop has Stata sleep for 1 second (1000 milliseconds), then checks if the file auto.dta exists in my Documents folder. The confirm file command will return the error code 7 until the file is found. Once the file is there the confirm file command returns error code 0, which means no error. The capture command is added to the confirm file command so that an error doesn’t break the loop, but the error code is still saved. We then reference the error code in our if{} expression, used to end the while{} loop.

369 views0 comments

Recent Posts

See All