Announcement

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

  • Show name of do file (create log file named after do file)

    Hello everyone,

    is there a (simple) way to name the log files after the do files without changing the name of the log file after changing the name of the respective do-file? I read about the batch mode, but this is not an option for me because I have to run the do files via a remote job submission application (due to security restriction of the dataset), and therefore I cannot access the installation of Stata.

    One possible way I thought of is saving the name of the do file in a local and using this local to create the log file. However, I'm not aware of any command that is able to display the name of the current do file. Is there a command which shows me the name of the do-file?

    Thanks in advance.

    Best regards,
    Sebastian

  • #2
    As do-files can be nested, several do-files can be running at once. So the idea of the "current" do-file needs care.

    Something like this is frequently asked. If I recall correctly, Robert Picard has a characteristically smart answer to this, but I can't remember what it is.

    Comment


    • #3
      As Nick said, there's no hook to get the name of the currently running do-file. I suppose this is something that StataCorp could include in a future version.

      I wrote and always use project (from SSC) to manage all my work in Stata. With project, you run do-files from within a master do-file. Each do-file in the project can also call other nested do-files. The project command decides which do-files need to be run again, depending on what has changed since the last build. All do-files are automatically logged (using the do-file's name) so the question is moot with respect to log file names. But within any do-file you can do the following:

      Code:
      project, doinfo
      local doname "`r(dofile)'"
      
      * do stuff
      
      save "`doname'.dta", replace
      You can do this with project because it's in charge of running each do-file. Since project knows all about the do-files it runs, it can return name of the currently running do-file, even if that do-file was called from another do-file (which itself may have been called by another do-file, etc).

      Another big bonus of using project is that it aligns Stata's current directory with the directory that contains the currently running do-file. So most of the time, you simply can load and save files using a simple file name, no file path required. This makes all projects eminently portable, and the moved project will run without having to change any file paths.

      Comment


      • #4
        A general approach is to use a do-file that accepts the name of a do-file as an argument and opens a similarly-named log file before running the do-file. Here's the skelton of what I do for my production work, although I use a ado-file, and the program therein takes care of a number of other things I typically want to do - for example, set more off while the inner do-file is running and turn it back on after it ends.
        Code:
        . type logdo.do
        log using `1', text replace name(`1')
        capture noisily do `1'
        log close _all
        
        . type hello.do
        display "hello, world!"
        
        . do logdo hello
        
        . log using `1', text replace name(`1')
        ------------------------------------------------------------------------------------------------
              name:  hello
               log:  /Users/lisowskiw/hello.log
          log type:  text
         opened on:  18 May 2016, 20:24:05
        
        . capture noisily do `1'
        
        . display "hello, world!"
        hello, world!
        
        . 
        end of do-file
        
        . log close _all
              name:  hello
               log:  /Users/lisowskiw/hello.log
          log type:  text
         closed on:  18 May 2016, 20:24:05
        ------------------------------------------------------------------------------------------------
        
        . 
        end of do-file
        
        . type hello.log
        ------------------------------------------------------------------------------------------------
              name:  hello
               log:  /Users/lisowskiw/hello.log
          log type:  text
         opened on:  18 May 2016, 20:24:05
        
        . capture noisily do `1'
        
        . display "hello, world!"
        hello, world!
        
        . 
        end of do-file
        
        . log close _all
              name:  hello
               log:  /Users/lisowskiw/hello.log
          log type:  text
         closed on:  18 May 2016, 20:24:05
        ------------------------------------------------------------------------------------------------
        
        .

        Comment

        Working...
        X