Announcement

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

  • Postfile does not work after user written command

    Hi all,

    For some reason Stata does not find my postfile after a user written command is executed. The really weird thing is that the first time I execute my do-file (let’s call it do-file A) the code does NOT run and an error message saying, “post postname not found” appears. However, if I execute do-file A again immediately after the first time, the code DOES run. So far, not big deal. The problem however arises when do-file A is called or used by another do-file (let’s call it do-file B). No matter how many times I execute do-file B, it will never run and I always get the same error.
    Do-file A is something like this:

    Code:
    drop _all
    postfile f str15(country year text) using file, replace
    local countries "pry hnd"
    local years "2005 2007"
     
    foreach country of local countries {
                foreach year of local years {
                            user_command [syntax]
                            post f ("`country'") ("`year'") ("this is a test")
                }
    }
     
    postclose f
    use file, clear
    exit
    I already checked the user written command (user_command.ado) and it does not have any “clear” or “clear all”, it only has “drop _all” and “preserve.” Also, neither do-file A or B has any “clear” or “clear all.”

    Any help would be highly appreciated.
    Thank you in advance,

    Pablo
    Best,
    Pablo Bonilla

  • #2
    I pasted your code into my do file editor and ran it with a couple of different user-written commands. I can't reproduce the problem: it runs just fine.

    From past experience with postfiles, which I use a great deal in my work, if you are developing your code as you go along and you run it, and then re-run it after it bombs somewhere and you make a change, if there was an open postfile at the time Stata stopped, that can mess things up with future postfiles. So in my do-files and ado-files, before I issue my first -postfile- command I always write -capture postutil clear-. That way the whole postfile mechanism starts out with a fresh slate. I don't know if that will solve this problem, but it's worth a try.

    Comment


    • #3
      What seems to me is that some external program such as Dropbox might be using your user written file soon after it is created, while after a second or so when Stata tries to access it, it might not get access to it. Try changing your directly to non-Dropbox directly or somewhere where other programs do not access the file.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Attaullah's guess sounds unlikely to me. I think you would know if you had also given the same filename to some other program.

        I would back Clyde's horse here. I particularly flag your use of a permanent name for your handle. Good practice here is to use a temporary name. That way, you are assured it is unique and transient. Some problem with an earlier attempt with the same handle could make Stata think it's still in use elsewhere.

        On a different note: the code example given is implausible as well as not reproducible. It's strange to run a user-written program repeatedly in a loop but not use any of its results. That may be irrelevant, but it is also possible that you simplified out of the picture some detail in the real code that is in fact pertinent.

        Comment


        • #5
          Hi all,
          Thank you for your responses.

          Clyde, thank you for the idea of the –capture postutil clear-. However, I put it at the beginning of my code and it didn’t work. Still, I think there is something in my user-written command.

          Attaullah, I have BOX not Dropbox, and I stopped the synchronization in order to check whether that is the problem and still it is not working. Thank you though

          Nick, as you said, the code that I posted is a super simplification of my real code. Our actual is something like this: relative big user-written command Alpha (3000 lines) is called by another big user-written command Beta (2000 lines). Right after Beta calls Alpha, the post file displays the error. Also, I use temporal names to call the postname and the file that the postfile generates, but I didn’t put them here for simplicity. My bad, I should have done so in order to avoid confusion. Thank you for your comments

          Now, notice that if I changed at beginning of my code the –drop _all- for –clear all-, no matter how many time I try to run my code, it never runs, which is weird because when I use –drop all- it does NOT run the first time but it does run the subsequent times.

          In short, I think that there is something in my Alpha user-written command that is erasing the information in my postfile only when I run it the first time. However, when I include my Alpha command and my postfile inside the user-written command Beta, it never works.

          I’d highly appreciate your help,

          Pablo.
          Best,
          Pablo Bonilla

          Comment


          • #6
            Well, if you think the problem is in your Alpha.ado, I would suggest -set trace on- before the call to Alpha and see what's going on inside it. If nothing else, you will find out where inside Alpha the problem is coming, and there's a good chance the trace will tell you what you need to know to figure out why.

            Comment


            • #7
              Clyde,

              I tried that, but given that it is a super long command (about 3000 lines) the trace is extremely long and there is no way (or at least I don’t know a way) to know where the problem is because the Alpha command runs fine. The problem is when after running the Alpha command the post command does not find my postfile because apparently it was “erased” by my Alpha command somewhere. The problem is that my Alpha command does not have any “clear” or “clear all”

              Thank you for your help though

              Pablo
              Best,
              Pablo Bonilla

              Comment


              • #8
                Can the mythical "user written command" be named? If not available publicly, can it be revealed?

                While postfile should work in principle, troubleshooting your situation may take a while. Consider alternatives to postfile, such as file write or perhaps accumulating results in a matrix. It will either work, or not work, but then it would be easier to troubleshoot (imho).

                In some cases you can use a file freeze method to get a clue of the culprit in the complex project, but I don't know the details of your commands, and whether it is going to be fruitful.

                The nature of the post file might be important. not sure how. but tell us what you know. Is this a temporary file? is this an exogenous argument? does it have absolute path? relative path? do alphas/betas/whatever change directories?

                as before, why do we have to guess? Clyde has already established, that your minimal code does not reproduce the problem. Keep it minimal, but bring it to the point where a person having Stata and nothing else can obtain the same error message on the screen as you do.

                Best, Sergiy

                PS: 3000 lines? consider refactoring and also review for efficiency (includes optionally: ask someone to review the code for you and provide feedback).

                Comment


                • #9
                  Sometimes when I have to find an obscure problem in a long file of code, I try commenting out portions of the code and running it to see if the problem persists or disappears. That can be difficult to do if there are a lot of local macros defined in one part of the code and used in another, but then successively commenting out larger sections of the end of the code can get around that. It's primitive, admittedly, but if you have nothing else to go on, worth a try.

                  Another thing you might try is changing the top level code to replace the handle f with a tempname and the postfile file with a tempfile and see if that makes a difference. I don't expect it will, but also worth a shot.

                  Comment


                  • #10
                    I ran into a similar problem and then found that one of my programs contained a "clear all" command. Changing it to "clear" resolved my issue.

                    Comment


                    • #11
                      Hi Matthew, that was precisely the problem. My user written command had a "clear all" somewhere and it was braking my postfile.... Thank you all for all your help.

                      Best,
                      Pablo
                      Best,
                      Pablo Bonilla

                      Comment


                      • #12
                        This is documented in -help clear-,

                        "clear all and clear * are synonyms. They remove all data, value labels, matrices, scalars, constraints, clusters, stored results, sersets, and Mata functions and objects from memory. They also close all open files and postfiles, clear the class system, close any open Graph windows and dialog boxes, drop all programs from memory, and reset all timers to zero."


                        Comment


                        • #13
                          Beware of version control here, especially in user-written programs. With version set to 9 or lower, clear will act the same way as modern clear all.

                          Best
                          Daniel

                          Comment

                          Working...
                          X