Announcement

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

  • Obtain name of current do-file

    I want to tokenize lines from the current do-file in the manner of John Thompson's wbsmodel.ado but not requiring the user to type in the name and path of the current do-file. Is the do-file's name available inside Stata?

  • #2
    I think this has been asked before, but cannot find the reference within a minute. I am not one hundred percent sure, but I think the answer has to be no. Here are three reasons why:

    1. do-files might call other do-files (which in turn call other do-files, which in turn ...). What is "the" (current) do-file then? The top level do-file? The lowest level do-file? Some file in between?

    2. As far as I know, Stata copies do-files to temporary files and runs these files. At least this is true if you have a do-file open. This is why you can make changes to the file while it is executed.

    3. Somehow related to 2, what if you run a do-file that has not yet been saved?

    Best
    Daniel
    Last edited by daniel klein; 07 Jun 2015, 06:11.

    Comment


    • #3
      Robert Picard wrote on Statalist in 2013:

      I have written a program that does this (get the do-file's name from within the do-file) and much more. It is called -project- and is currently available from my web site by typing in Stata:

      net from http://robertpicard.com/stata

      and clicking on the -project- link.
      You can read the entire message at http://www.stata.com/statalist/archi.../msg00026.html.

      Comment


      • #4
        Thanks, that's been very interesting to look seriously at -project-. I had heard good things about it. Although it is very impressive, it seems that there are some restrictions in the context I'm considering, such as choosing the master do-file from a dialog, so I'm left wondering whether the 2013 quote was a bit of a misunderstanding. It can indeed trace do-files named inside the first do-file, and all their descendants, but not (as far as I can tell) get the name of that first do-file. I'm looking into whether StataStan users (https://github.com/stan-dev/statastan/tree/alpha-test) could write their Stan model in a comment block (a la Thompson). I'm now thinking of reading lines from the most recent "S*" file in c(tmpdir) instead, which would mean requiring the user to highlight and do/run the model comment block at the same time as the -stan- command (and could still go wrong if other processes are dumping files named S* in there too), which is an unfortunate restriction for them but not as tiresome as typing in the do-file name and path, I think. Maybe I should offer both approaches, plus naming a separate ASCII model file.

        Comment


        • #5
          Another advantage of reading from c(tmpdir) is that the user doesn't have to save the do-file before do-ing...

          Comment


          • #6
            I don't understand this thread. I tried to find "wbsmodel.ado" but failed. I know nothing of StataStan. You appear to want a do-file to digest itself. I'm scratching my head here.

            With respect to project (from SSC), you do not need to use a dialog to define a project. It's just a shortcut to the setmaster() option and using the dialog avoids having to type the full path to the master do-file. You also can, from the master do-file, get the name of the do-file although I don't see the advantage there.

            You could conceivably construct a do-file that would set-up and then build a project. In that case, project would have no way to know the name of the do-file that called it. I don't know why anyone would want to do this.

            Comment


            • #7
              Hi Robert, here's where I'm coming from. Stan is a Bayesian modelling package (mc-stan.org) and, like BUGS, you can write out a model in a conditionally specified simple language. John Thompson made the most widely used Stata-to-BUGS interfaces and came up with the idea that the Stata user could write their model (in BUGS language) inside a comment block in the do-file. His command wbsmodel then (given the name of the do-file) scans through and picks out the model, which is convenient as a way of keeping everything in one do-file. I'm making StataStan, which does just what you'd imagine, and I wanted to offer an inline model-writing option like John's, but wondered if there was a way that the user could avoid having to name (and maybe give the path to) the do-file. This would require Stata to know where to get the contents of the do-file. So, I wonder if you cracked that problem in -project- : can a project command inside a do-file obtain the address of that very same do-file, or does it have to be typed in?

              Comment


              • #8
                Nope, project can track the currently executing do-file (which can be nested within many other levels of do-files) because it's in charge of running each do-file (via the project, do() command) and it keeps track. There's no way that I know to get the currently running do-file from Stata.


                Comment


                • #9
                  Thanks, I thought so. Maybe someone from StataCorp knows a trick but I'm going with the c(tmpdir) approach for now.

                  Comment

                  Working...
                  X