Announcement

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

  • Sending output to file _instead of_ Results window

    I have a do-file that produces a lot of output from various types of commands. I would like to store this output in a file to look at later, but without having it clutter up the Results window.

    I am aware of the log command, but I seem to have to choose between two scenarios: if I run the program quietly, then output is not shown in the Results window, nor gets recorded in the log file; or if I do the program noisily, I end up with the output in both places. I really want to avoid cluttering up my Results window. For instance, I would like to be able to scroll up to visually see the output of commands I entered interactively before, but these previous results disappear from the window due to the sheer volume of output being generated by my do-file.

    I am also familiar with the file command to pipe specific things to a text file, but I am not sure using it is practical to capture output from the variety of commands that generate output in my do-file.

    I would really appreciate your thoughts on how to do this. Thank you in advance!
    Last edited by Hemanshu Kumar; 04 Feb 2022, 01:25.

  • #2
    I can't think of a way to stop Stata from sending things to the Results window and yet still get the output into a file without resort to the -file-command and a pretty complicated program to parse the output. That sounds like a nightmare of programming to me, too. But here's an imperfect solution:

    Code:
    capture log close _all
    
    log using first_log, replace
    // doing stuff
    log close
    
    log using second_log, replace
    // doing stuff that produces a lot of output
    log close
    
    log using first_log, append
    view first_log.smcl
    // continue with other stuff
    
    log close
    The massive output is written to both the Results window and the second log file. But then the first log file resumes, and is opened in the viewer where you can scroll to see previous commands and their output. Also, you can at that point hit Refresh on the view to see the most up-to-date work.

    Comment


    • #3
      How about run it in batch mode? See Append B "Advanced Stata usage" in the Getting Started manual. Also this faq: https://www.stata.com/support/faqs/windows/batch-mode/

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        I can't think of a way to stop Stata from sending things to the Results window and yet still get the output into a file without resort to the -file-command and a pretty complicated program to parse the output. That sounds like a nightmare of programming to me, too. But here's an imperfect solution:

        Code:
        capture log close _all
        
        log using first_log, replace
        // doing stuff
        log close
        
        log using second_log, replace
        // doing stuff that produces a lot of output
        log close
        
        log using first_log, append
        view first_log.smcl
        // continue with other stuff
        
        log close
        The massive output is written to both the Results window and the second log file. But then the first log file resumes, and is opened in the viewer where you can scroll to see previous commands and their output. Also, you can at that point hit Refresh on the view to see the most up-to-date work.
        In addition to the helpful advice and example from Clyde, you may open as many log files as you wish, and can output to them simultaneously, and you can temporarily turn on/off logging. See -help log- for details.

        That said, I think this kind of workflow is problematic and won't be effective. Log files are useful records of what was done. Sometimes it makes sense to quiet commands to suppress output (see -help quietly-) or else make the verbose (see -help noisily-). But, otherwise, if you want to save only some pieces of the log for review, then separate log files is one way, or else dumping that output into a Word/Excel/PDF might be more useful in the long-run.

        Comment


        • #5
          Originally posted by Scott Merryman View Post
          How about run it in batch mode? See Append B "Advanced Stata usage" in the Getting Started manual. Also this faq: https://www.stata.com/support/faqs/windows/batch-mode/
          Actually, Scott's solution works brilliantly for me. I can run the do-file in batch mode, and then just reload the key dataset I'm using interactively to continue playing around with it.

          Thanks to Clyde and Leonardo for their suggestions too. While opening and closing multiple log files would work in principle, it requires too much forethought (this is all useful mainly when I'm "playing around" interactively...) and feels too clunky.

          Would anyone know if the batch mode allows us to specify the name and location of the log file that is generated? I looked up the Stata pages on this online, but they're silent on how to achieve this.
          Last edited by Hemanshu Kumar; 04 Feb 2022, 22:12.

          Comment

          Working...
          X