Announcement

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

  • Your solution for identifying the "do file" that created an output in Stata

    Please share your creative solution for this.

    I often save Stata output as a PDF (by doing "select all" in the Output screen, and then "Print to PDF").

    However, later, I sometimes am looking at one of my output PDFs and want look at the "do file" that created it.

    Is there some way to get the filename of my "do file" into the output?

    If not, is there some other creative solution to connect the output to the "do file" that created it?

    Thanks.

  • #2
    Instead of doing a manual operation to save your results as a PDF, you can have your do file log your results and then translate the log to PDF. Like this:
    Code:
    /* BEGINNING OF DO-FILE */
    capture log close
    log using name_of_your_log_file, replace
    
    // THE PROBLEM SOLVING CODE GOES HERE
    
    log close
    translate name_of_your_log_file.smcl name_of_your_log_file.pdf, replace
    exit
    /* END OF LOG FILE */
    That way your PDF will automatically have the same name as your log file. And, if you give your log file the same name (other than the .do extension) as your do-file, all three will always be readily associated with each other.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Instead of doing a manual operation to save your results as a PDF, you can have your do file log your results and then translate the log to PDF. Like this:
      Code:
      /* BEGINNING OF DO-FILE */
      capture log close
      log using name_of_your_log_file, replace
      
      // THE PROBLEM SOLVING CODE GOES HERE
      
      log close
      translate name_of_your_log_file.smcl name_of_your_log_file.pdf, replace
      exit
      /* END OF LOG FILE */
      That way your PDF will automatically have the same name as your log file. And, if you give your log file the same name (other than the .do extension) as your do-file, all three will always be readily associated with each other.
      Thank you for your suggestion.

      I tried it out.

      It's a good solution, however there were 2 things I'd rather not deal with:
      1. The manual "select all and print to PDF" produces a cleaner PDF, with table alignments etc. that look identical to the Output screen.
      2. I was hoping that name_of_your_log_file would dynamically pick up the filename of my "do file". Sadly, this wasn't so. In this solution, I'd have to manually change name_of_your_log_file each time I create a new "do file". I create lots of "do files" and will surely forget to do that sometimes, leading to a mess where multiple outputs point to the same "do file".
      But I do appreciate your suggestion. I may have to adopt it if nothing better emerges.

      Comment


      • #4
        Stata can’t dynamically get the name of the currently running do file. The best you can do, which is a very good practice, is name the log file the same as the do file. Yes it’s one line of added code, but that’s not hard for something to d once for each do file.

        There is a problem with your manual approach to creating pdfs and that is what happens when you run out of output display buffer. You’ll need a programmatic solution like Clyde’s to keep everything.

        Comment


        • #5
          Originally posted by Leonardo Guizzetti View Post
          Stata can’t dynamically get the name of the currently running do file. The best you can do, which is a very good practice, is name the log file the same as the do file. Yes it’s one line of added code, but that’s not hard for something to d once for each do file.

          There is a problem with your manual approach to creating pdfs and that is what happens when you run out of output display buffer. You’ll need a programmatic solution like Clyde’s to keep everything.
          > what happens when you run out of output display buffer

          Interesting. Good to learn about this. Thank you.

          Comment


          • #6
            Re: #3. If you look at -help translate- you will see that you can select options to set the margins when you -translate- to PDF.

            Yes, you do have to specify a name for your log file with each do-file. I have to admit that this is such an unwavering and routine part of my workflow that I didn't even consider the possibility that you don't do that. It is good practice to log all of your do-file outputs: if the code is worth memorializing in a do-file, the results are worth memorializing in a log file. And to avoid confusion you should give them the same name (other than the .do and .smcl filename extensions). Those first two lines of code I posted in #2 are the first two lines of every single do-file I write. No exceptions. I do occasionally give the log file a different name from the do-file in the special circumstance where I am going to create separate log files for different parts of the do-file, but that arises very infrequently in my workflow.

            I would also say that using the graphical user interface is fine when you are just exploring things, and it can sometimes be a good way to learn what the code corresponding to different ways of using a command looks like. But for serious work, and certainly for anything that you are going to send to others, you should only use code. Anything that goes to others should have a beginning-to-end audit trail. Log files provide that; manual operations do not.

            Comment


            • #7
              One administrative detail: please note the long-standing request that we use real names on this forum. It seems unlikely that you are named after a Bombardier plane. You can find details of how to request a name change in the FAQ.

              Comment

              Working...
              X