Announcement

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

  • Can the user decide whether to loop code or only run it once?

    In summary, my problem could be solved if I could save a bunch of code to a macro, then stipulate when the code is run (after meeting criteria in if statements).

    In my .do file, I am asking the user some questions about what regressions they want to run, using display _request(macroname), then I interpret their answer (which is either yes or no in most cases) and define another macro that appears in the regression code. I'm doing this because there are lots of different options of which regression to run, and this makes it easy to select which one to run. Now I want to automate a process which loops through all the different options, and I thought it would be nice to add in an extra question at the start asking whether the user also doesn't want to bother with answering questions, but to run the 64 combinations (the output of which will save to various appropriately named different Word documents).

    So I have the code for the regressions that needs to be run, but it needs to either be run only once after asking 5 questions, or looped.

    Can I save the code to a macro and then stipulate when the code should be run?

    FYI, the code I'm talking about is this: https://pastebin.com/fLpDt19m

  • #2
    Cross-posted at https://stackoverflow.com/questions/...-once-in-stata

    Comment


    • #3
      It appears to be that the problem you face is that your code consists of multiple commands, so you put it in a single local macro and execute it. You could use file to write a (temporary) .do file, and at the end do that, or you could work with multiple macros.

      However, I suspect that you will end up with a more user friendly result if you do this with a dialog (see: help dialog programming).
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        I can probably solve this by ​​putting the code in a separate .do file and then using the do command to call the code.

        Comment


        • #5
          That sounds like you would end up with very hard to maintain code.

          What are you trying to do? Who are your intended users?
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            If what you're trying to do is just give a user an option to choose between two options you could use a window stopbox:

            Code:
            capture window stopbox rusure "Do you want to run the code multiple times? Answering No will run the code once"
            if _rc == 0 [something something]
            The message box exits with a return code of 0 if the user clicks Yes, or exits with a return code of 1 if the user clicks No so you can base your path on that. You could either fork in the do file or, as you've suggested, nest conditional do files in the script with an if else (e.g. if _rc==0 do "path\loop_64.do" etc.)

            Stopboxes can get annoying. I often think they'll be helpful but generally end up removing them after a while and using the two do file approach you suggested. It depends a bit on your circumstances and users as Maarten has asked.
            Last edited by cammcdermaid; 18 May 2018, 07:43.

            Comment


            • #7
              My do files won't be used by more than a handful of people in reality. It is a project that does analysis of data to answer a specific hypothesis, to give empirical evidence in a paper.

              Comment


              • #8
                That makes me really worried. It is critically important that any result in a paper is the exact result of a .do file. So that precludes the use of such an interactive strategy you choose. If the results are the results of some interactive and thus undocumented choices, then your results are meaningless. What makes results "scientific" is the fact that we know all the steps that led to that result, so we can talk about it and replicate the results. If part of the steps are lost, then the results can no longer be part of a scientific debate.

                For more viable strategies see: https://www.stata-press.com/books/wo...nalysis-stata/
                ---------------------------------
                Maarten L. Buis
                University of Konstanz
                Department of history and sociology
                box 40
                78457 Konstanz
                Germany
                http://www.maartenbuis.nl
                ---------------------------------

                Comment


                • #9
                  Hi, Maybe pushing too far on _request capabilities ... Are there any way to include a timer so If the user does not select a option after 5 seconds, it continues with a "default" value? thks

                  Comment

                  Working...
                  X