Announcement

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

  • Stata break button doesn't work. The run needs to stop without losing the data

    Stata break button doesn't work and the exit-command (Without clear) doesn't work either. The run needs to stop without losing the data. I ran several intensive commands and now I want to check the results I have so far instead of waiting 10 more hours for the .do file to finish.

    Is there a way to stop Stata? Normally I would shut it down using ctrl+alt+delete but this time I need to keep the results+data.

    Simple way to help me in less than a minute: If I stop the process with ctrl+alt+delete instead of terminating Stata with ctrl+alt+delete....would that leave Stata open with all my data and results I have so far? Could someone check that please by running an intensive command and then ctrl+alt+delete then processes then click on Stata then click on end process....so, ending the process of Stata and not ending the program Stata.
    Last edited by Victoria Rogers; 23 Oct 2014, 16:31.

  • #2
    Sorry, but I don't think you can do what you want. The manual says "If you click on Break while Stata is doing something—creating a new variable, sorting a dataset, making a graph, etc.—Stata stops what it is doing, undoes it, and issues an input prompt. The state of the system is the same as if you had never issued the command."
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      You will lose your partial results if you kill Stata using the task manager, unless you are saving them to a file in the hard disk in the process. Assuming you are running a loop, the only way to keep partial results would be to save partial results in each iteration.
      Jorge Eduardo Pérez Pérez
      www.jorgeperezperez.com

      Comment


      • #4
        Richard and Jorge, I don't need to keep the results of the current command which makes it impossible to use the break-button.
        I just want to keep the results of the other commands I used after I saved my data 10 hours ago. I'm about halfway now with the data processing and I want to check if everything is going as planned by browsing my data.

        I.e. after I saved, I made 3 variables with alphas and currently Stata is busy with another 4 variables, I want to keep the already generated alphas/3 variables. At the moment, I don't care about the last 4 variables.

        At the moment, I'm stuck in a loop and cannot use break but I don't care about the current loop. At least not until I've checked the results of the previous loops. (I'm stuck in a version of the really nice loop Clyde made for me, it might be stuck because it uses 'capture'.)
        Last edited by Victoria Rogers; 23 Oct 2014, 16:45.

        Comment


        • #5
          I don't know about this "impossible to use the break button". Sometimes I have to hit it a few times, but the running program always breaks sooner or later.

          Did those three variables take several hours to generate? If not, I would copy and save the commands that created them and rerun those.

          But keep trying with break. I've never seen a command that won't die sooner or later if I keep hitting break. If you use ctrl-alt-delete I think you will lose those three variables because Stata will terminate.

          You could always start a 2nd instance of Stata and try ideas out if you thought they might work.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Yes, those 3 variables took at least 18 hours to be generated.

            I literally hit the break button more than 50 times and I also tried to stop the command by using the break key on my keyboard in combination with the correct other key (ctrl or shift or alt) (tried that for at least 10 times). I also tried stopping the command by entering -exit- (without clear) because on the Internet I saw that Stata always ask to save your results before it closes Stata (if you use exit).

            Simple way to help me in less than a minute: Could someone run an intensive command and then ctrl+alt+delete then processes then click on Stata then click on end process....so, ending the process of Stata and not ending the program Stata.

            In this case, I want to generate more variables with alphas. After that I need to run regressions based on those variables. So, I cannot work with a 2nd instance of Stata at the moment even though it's a good advice, often I use 2 instances of Stata.
            Last edited by Victoria Rogers; 23 Oct 2014, 17:43.

            Comment


            • #7
              Well, you may be in luck. When I hit ctrl-alt-delete Stata asks me if I want to save changes to the data before exiting. Just be careful how you handle the prompts. Stata will exit but you'll be able to save the data with your 3 new variables first.

              You are going to lose your 10+ hours of work, so be sure that is what you want to do.

              FYI, I think you could have opened a 2nd instance. I just fired up stata, gave it a task I knew would take a long time, and then used ctrl-alt-delete to see what would happen if I tried to kill it.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              Stata Version: 17.0 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment


              • #8
                Originally posted by Richard Williams View Post
                Well, you may be in luck. When I hit ctrl-alt-delete Stata asks me if I want to save changes to the data before exiting. Just be careful how you handle the prompts. Stata will exit but you'll be able to save the data with your 3 new variables first.

                You are going to lose your 10+ hours of work, so be sure that is what you want to do.

                FYI, I think you could have opened a 2nd instance. I just fired up stata, gave it a task I knew would take a long time, and then used ctrl-alt-delete to see what would happen if I tried to kill it.

                Just to be 100% sure, did you try to close Stata the program or Stata the process (that sounds strange, but makes it a lot more clear)
                The current command only has been busy for 2 hours.

                Comment


                • #9
                  I closed Stata the application, not the process. You get warning messages either way so read them carefully.
                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  Stata Version: 17.0 MP (2 processor)

                  EMAIL: [email protected]
                  WWW: https://www3.nd.edu/~rwilliam

                  Comment


                  • #10
                    It doesn't work unfortunately, I tried to save it. Even give it a new filename. When I tried to open the new file, it wasn't in the place where I saved it. So I cannot save before the code stops running. I even tried to ctrl+alt+delete then terminate the Stata process, but then a warning showed up that the program would be closed and everything would be lost.

                    Comment


                    • #11
                      Code:
                       
                       gen alpha = . forvalues j = 1/`=_N' { capture ereturn clear capture regress return risk_premium if UniqueID == UniqueID[`j'] /// & inrange(monthyear, monthyear[`j']-23, monthyear[`j']) capture replace alpha = _b[_cons] in `j' }
                      If you run that code, that code is 'impossible' to stop until it's done
                      so you have to wait until it's done or you'll lose all the other things you did before you ran that code (the things you didn't save before running the above code)

                      Comment


                      • #12
                        That's it? Thats the code that takes 18 hours to run and won't let you break out of it? I don't know why it is so fussy but I will be sure never to run it. ;-) Hope you are able to eventually get what you want. Your boss needs to either give you smaller datasets or a far more powerful machine. Good luck!
                        -------------------------------------------
                        Richard Williams, Notre Dame Dept of Sociology
                        Stata Version: 17.0 MP (2 processor)

                        EMAIL: [email protected]
                        WWW: https://www3.nd.edu/~rwilliam

                        Comment


                        • #13
                          You can use an external text file with a continuation flag (0 = pause and let me inspect; 1 = keep going without pausing) that's checked at each iteration of the loop in order to pause Stata so that you can inspect the interim results before proceeding. You'd toggle whether to pause after the next iteration by opening the text file in a convenient text editor and setting the flag to your desired action. See the example do-file below. It should enable you to do something close to what you want. (I haven't tested it, and so it might have syntax errors etc.)

                          Also, as Richard says, it seems that you have an enormous dataset that Stata must pore through during each regression. You might want to load just that subset of the larger dataset before fitting the linear model.
                          Code:
                          *
                          * Begin here
                          *
                          version 13.1
                          clear *
                          set more off
                          
                          pause on
                          
                          *
                          * Set up your external loop-controller file
                          *
                          tempname file_handle
                          file open `file_handle' ///
                              using "<a convenient directory>:continue_flag_file.txt", write text
                          file write `file_handle' "0" // _newline(1)
                          file close `file_handle'
                          
                          *
                          * Load pertinent part your dataset, fit model and optionally inspect before next loop
                          *
                          tempfile Results
                          describe using <dataset>
                          forvalues j = 1/r(N) {
                          
                              // Get next UniqueID
                              use UniqueID in `j' using <dataset>, clear
                              local UniqueID = UniqueID[1]
                          
                              // Load just the data that you need this loop iteration
                              use return risk_premium if UniqueID == "`UniqueID'" using <dataset>, clear // change if UniqueID not string
                          
                              // Do your thing
                              quietly generate double alpha = .
                              display in smcl as result _newline(2) "Loop `j'", "UniqueID `UniqueID'"
                              regress return c.risk_premium
                              quietly replace alpha = _b[_cons]
                          
                              // If no desire to inspect, open continue_flag_file.txt and change its zero to one
                              // during lengthy regression (before Stata gets to this point in loop)
                              file open `file_handle' ///
                                  using "<a convenient directory>:continue_flag_file.txt", read text
                              file read `file_handle' continue
                              file close `file_handle'
                              if !`continue' pause // Loop is paused; you may inspect interim results here
                              else {
                                  // No op
                              }
                              
                              if `j' == 1 save `Results'
                              else {
                                  append using `Results'
                                  save `Results', replace // Final results will be in memory after last loop
                              }
                          }
                          
                          exit
                          If it is more natural to you to have a "pause" flag than a "continuation" flag, you can easily reverse the sense of the flag in the external loop-controller text file, and correspondingly reverse the logic of the flag-monitoring code inside the loop.

                          You can add the "monthyear" restrictions to the code above, by loading not just UniqueID[`j'], but also monthyear[`j'] in the pilot data-load at the top of the loop, and have two local macros, one holding UniqueID[`j'] and the other holding monthyear[`j']. You can then add both conditions to your use . . . if . . . using line.

                          If your dataset is enormous, you might be better off putting it into a database where you can place indexes on your selection columns (UniqueID and monthyear), so that subsetting is faster. For that, you will need to use Stata's ODBC suite and a pass-through SQL statement (odbc load, exec("SELECT result risk_premium FROM <Table> WHERE UniqueID = '`UniqueID'' AND monthyear BETWEEN . . .;") connectionstring(. . .) clear in your loop in lieu of the use line.

                          Comment


                          • #14
                            One thing that I neglected to mention is that you cannot reference the tempfile during the paused loop. If you want to be able to quit without loosing results for any previous iterations, you should place the pause after the append in the loop. (Appending places the current loop's results at the top of the dataset in memory for easy inspection.) This way, if you wish to stop execution after inspection of the interim results, you can type BREAK at the command line when paused. This will quit execution of the do-file leaving the completed loops' results in memory.

                            As an alternative, you can also change the tempfile to a permanent file, and you can then reference it in order to append previous loops' results after pausing and typing BREAK.

                            Comment


                            • #15
                              Originally posted by Richard Williams View Post
                              That's it? Thats the code that takes 18 hours to run and won't let you break out of it? I don't know why it is so fussy but I will be sure never to run it. ;-) Hope you are able to eventually get what you want. Your boss needs to either give you smaller datasets or a far more powerful machine. Good luck!
                              Yes, that's it, but it takes 6 hours each time I run that code, so 18 hours was the total at that time

                              @Joseph, your code looks really nice and I'll definately try to use it in the nearby future, however, can I also use your code to stop currently running codes. So codes which I started to run before setting up your code?

                              Comment

                              Working...
                              X