Announcement

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

  • do files and working directories

    I would love for there to be command or code that I could put at the top of a .do file that had the functionality "change the working directory to whatever the directory is of the .do file being executed." Is there a way to do this? As far as I can tell, the "do" command in Stata doesn't story the location of the do-file that it is executed, but I don't know if I'm wrong about this, or if Stata or somebody more clever than I am has figured out some work-around.

  • #2
    For some leads and approaches, see http://www.stata.com/statalist/archi.../msg00026.html, and also
    http://www.statalist.org/forums/foru...urrent-do-file

    Regards, Mike

    Comment


    • #3
      The problem with dynamically changing Stata's current directory is that the concept of the currently running do-file is fuzzier than it seems because a do-file can itself call another do-file. Both are, strictly speaking, currently running at the same time. As you noticed, Stata does not offer a hook that returns the path to the "top-level" do-file that is currently being processed. Even if it did, you would have the problem of how to restore the current directory to the one of the calling do-file once the current "top-level" do-file has terminated.

      As Mike has pointed out, you can use project (from SSC) to manage the execution of your do-files. With project, the current directory is always aligned with the current "top-level" do-file. project alsoautomates the logging of do-files. In addition, project tracks do-file dependencies and will skip running all do-files that haven't changed as long as their respective inputs/outputs haven't changed either. With project, you do not run do-files in an ad hoc and out of context way; all do-files are children of the project's master do-file and are usually organized in a hierarchical way with do-files calling other do-files.

      With project, you never need to use a full path name for any file operation. Since project takes care of changing the current directory for all do-files it runs, a do-file can access all files in the same directory by file name only. If files are in another directory, you can use a path that is relative to the project's master directory. This makes it possible to create a project that can be moved to a new computer, shared with others, or posted online. Absolutely no change is needed for others to be able to run the project, irrespective of their platform and directory structure.

      There are many other features of project, including the ability to check that you can replicate exactly the results produced by your code, not only the final tables but all datasets, log files, and all other files created by the project. There is no other utility (in Stata or user-written) that can provide this kind of functionality at this time.

      To install project, type in Stata's command window

      Code:
      ssc install project
      and then study the help file using

      Code:
      help project
      There are two demo projects that can also be downloaded after you install project. To get them, type

      Code:
      net get project

      Comment


      • #4
        Dear Robert,

        I love the idea of project! I´ve been trying to run your examples v9 and v12, but I always get the error message "no project being built" after executing "project, do("ado/link_ado_files.do")". I am working with STATA 14, do I have to make any adaptation to the master or any of the other dofiles?

        Best, Susann

        Comment


        • #5
          It looks like you are trying to run a single do-file independently. Here's what I get if I copy the command to Stata's Command window:

          Code:
          . project, do("ado/link_ado_files.do")
          no project being built
          r(198);
          With project, you never run do-files separately, you always run the project as a whole. The first step is to define the project by typing the following in the Command window:

          Code:
          project, setup
          This brings up a dialog that lets you select the master do-file for the project. You then navigate to the "examples_v9" directory and select "ex9.do" and click "OK". This is the output in the Results window on my computer:

          Code:
          . project, setup
          
          . project , setmaster("/Users/robert/Desktop/examples_v9/ex9.do")
          +---------------------------------------------------+
          |                      Project                      |
          |                                                   |
          | Name  Log Type  Full path to directory            |
          |---------------------------------------------------|
          | ex9       SMCL  /Users/robert/Desktop/examples_v9 |
          +---------------------------------------------------+
          Now that the project is defined, your next step is to build the project. You do this by typing

          Code:
          project ex9, build
          Note that you'll encounter the following error message

          Code:
          .         replace diag1 = "230.6" if patid==2
          (1 real change made)
          
          .         
          .         drop prob
          
          .         
          .         icd9 clean diag1
          diag1 contains invalid ICD-9 codes
          r(459);
          
          end of do-file
                name:  plog_65
                 log:  /Users/robert/Desktop/examples_v9/data-management/icd9.smcl
            log type:  smcl
           closed on:   9 Nov 2015, 09:32:44
          because Stata has changed the dataset used by the "icd9.do" do-file (in the "data-management" sub-directory) after the demo projects were posted. Since this is just a demo project, you can simply edit "icd9.do" and place an

          Code:
          exit
          near the top to skip the section that generates an error. I'm current working on a new version of project and the demo projects will be updated when the next version is posted to SSC.

          Comment

          Working...
          X