Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exit Stata after run do-file

    Hi! I'm running a Python code that opens Stata and runs a do-file. After the do-file is run I would like to continue with my Python code and for that I would like to add at the end of the do-file, a line that closes Stata. That's because the way I came up to continue running my Python code after the do-file is run is to check whtehter Stata is a process on Windows or not. As I cannot manage to close and exit Stata, Python checks whther Stata is a process and the answer is still yes.

    I hope I've been clear with the explanation of my problem. Thanks

  • #2
    It's a bit subtle, but
    Code:
    help exit_program
    tells us that
    Code:
    exit, STATA clear
    will cause an exit command issued within a do-file will cause Stata to exit, even if the dataset in memory has been changed and not subsequently saved, and return control to the operating system.

    Comment


    • #3
      William Lisowski is correct. However, the problem may be a little more complicated than just putting -exit, STATA clear- at the end.

      What do you want to do if the Stata program encounters an error condition and halts with an error message before reaching the end? The approach above will cause Stata to remain open, and your Python program will hang. That may, in fact, be a good way to handle the situation.* If, however, there are some error conditions where it would be acceptable to close Stata and let Python proceed, you would need to -capture- those errors and then conditional on a non-zero return code, have another -exit, STATA clear- command there.

      *Even here, it would probably be more elegant to have the Stata program exit with a non-zero return code, and have Python pick that up and respond in some appropriate way. (I am not a Python programmer and I don't know whether Python can actually do that--perhaps my brain is damaged by Unix-think.)

      Comment

      Working...
      X