Announcement

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

  • Is it possible to tell Stata to always start log file of the current session and save it to a specified folder?

    Hi all,

    Not to repeat my question, the point is to have a copy of every session (perhaps naming each one with the date and specific time the log was created) in case Stata crashes or one forgets to create a log or a do file to keep track of steps taken while fooling around with the command line. I am aware of the fact that I could just try to create a habit of doing it myself, but I was wondering if it is possible for this to happen automatically. Any thoughts?

    Thanks in advance,

  • #2
    I know of no way to do it automatically; however, below I show a short ado file that I use (based on one originally written by Bill Gould) that works for me since I only open Stata by clicking on the icon in my dock (I use a Mac); you should not either click on a "do" file or a "dta" file when using this as it would mean that some of your work was not logged; you may also want a different type of log file then I am using:
    Code:
    program define ii
    version 6.0
        if "`1'"=="" {
             di in red "no directory named"
             exit
        }
       cd "/Volumes/`1'"
       log using stata.log, append
    end
    thus, when I open Stata, I type:
    Code:
    ii folder/directory name
    if your folder path has spaces be sure to enclose in double quote marks

    Comment


    • #3
      This (or something like it) appears in the Stata Manual somewhere:

      If you want to make sure you don’t accidentally overwrite the log files, the Stata manuals suggest a command like

      log using `: display %tCCCYY-NN-DD-HH-MM-SS ///
      Clock("`c(current_date)' `c(current_time)'","DMYhms")', ///
      name(default_log_file)

      This will result in a log file with a name like 2016-05-15-08-12-57.smcl. If you are wondering why that works, if we break out the display part of the command we get

      . display %tCCCYY-NN-DD-HH-MM-SS Clock("`c(current_date)' `c(current_time)'","DMYhms")
      2016-05-15-08-12-57

      The command that is actually executed is then

      log using 2016-05-15-08-12-57, name(default_log_file)
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Carlos,

        You could put the following in your profile.do (which executes every time you open Stata):

        Code:
        local c_date = c(current_date)
        local c_time = c(current_time)
        local c_time_date = "`c_date'"+"_" +"`c_time'"
        local time_string = subinstr("`c_time_date'", ":", "_", .)
        local time_string = subinstr("`time_string'", " ", "_", .)
        log using "log_`time_string'.log"
        Alternatively, if you don't want to log all of your results, but just want to be able to recreate your commands, you could use cmdlog instead of log.

        Regards,
        Joe

        Comment


        • #5
          Thanks to all of you. I think your suggestions are more than enough to make this happen.

          Comment


          • #6
            Carlos: you might also be interested in built-in command cmdlog. I use it in my profile.do file (run automatically when Stata starts) so that all commands issued in my interactive sessions are saved in a file. This is separate from any do-file that I may use at the same time

            Comment


            • #7
              I have a related question. Does this mean that you always have to update/rerun the code for every session? I use these valid codes for saving my work:

              local date: di %tdN-D-CY daily("$S_DATE", "DMY")
              di "`date'"
              local time = "$S_TIME"
              di "`time'"
              local date_time = "`date'"+"_" +"`time'"
              di "`date_time'"
              local timestamp = subinstr("`date_time'", ":", ".", .)
              di "`timestamp'"

              OR

              local date: di %tdN-D-CY daily(c(current_date), "DMY")
              di "`date'"
              local time: di c(current_time)
              di "`time'"
              local date_time = "`date'"+"_" +"`time'"
              display "`date_time'"
              local timestamp = subinstr("`date_time'", ":", ".", .)
              display "`timestamp'"

              and use `timestamp' to save my documents, but it seems that the time is usually for when I created the macro...does this mean I have to keep updating recreating the macro to make this work e.g. everytime I want to save an output, dataset, etc with the "timestamp" it seems quite counterintuitive and you would think that by using the "current date" code, it would automatically update.

              Am I doing something wrong?

              Comment


              • #8
                You can write your code as a program and put it at the top of your do file or save it as an ado file in your personal ado folder. Then when your give the command get_ts, your new time stamp is available.

                Code:
                cap prog drop get_ts
                prog get_ts, rclass
                
                local date: di %tdN-D-CY daily(c(current_date), "DMY")
                di "`date'"
                local time: di c(current_time)
                di "`time'"
                local date_time = "`date'"+"_" +"`time'"
                display "`date_time'"
                local timestamp = subinstr("`date_time'", ":", ".", .)
                display "`timestamp'"
                
                return local ts `timestamp'
                end
                Then you have the new timestamp in the returned result r(ts):

                Code:
                get_ts
                save "my file name `r(ts)'
                
                get_ts
                save "new file name `r(ts)'
                
                get_ts
                save "another file name `r(ts)'
                Stata/MP 14.1 (64-bit x86-64)
                Revision 19 May 2016
                Win 8.1

                Comment


                • #9
                  Thanks to the comments I got from this post, here's the code I use, just change the PATH :

                  Code:
                  local c_date = c(current_date)
                  local c_time = c(current_time)
                  local c_time_date = "`c_date'"+"_" +"`c_time'"
                  local time_string = subinstr(trim(itrim("`c_time_date'")), ":", "_", .)
                  local time_string = subinstr("`time_string'", " ", "_", .)
                  local time_string = subinstr("`time_string'", "__", "_", .)
                  capture log close _all
                  capture cmdlog close 
                  log using "PATH\log_`time_string'", smcl name("log_`time_string'_smcl") append
                  log using "PATH\log_`time_string'", text name("log_`time_string'_text") append
                  cmdlog using "PATH\cmdlog_`time_string'", append
                  Save to a do file and put it in your PERSONAL folder in your ADO folder. I keep one copy of the log in smcl, another in text, and one copy of the command log. All of them named after the datetime of the moment I opened up Stata. Opens logs every time I open a session, regardless of whether I have another session open.

                  I hope it helps

                  Comment


                  • #10
                    Am I correct in assuming that any automatic logging that is done like this (e.g., starting with the session) will prevent the user's script from also creating log files?

                    Edit: No, it doesn't prevent this behaviour. I just tested it to prove it to myself.
                    Last edited by Leonardo Guizzetti; 21 Aug 2018, 10:19.

                    Comment


                    • #11
                      Hi Leonardo,

                      Not in this case. Stata will not give you problems if you started a log and gave it a name as in
                      Code:
                      log using "PATH\log_`time_string'", smcl name("log_`time_string'_smcl") append
                      For example, you can try this:
                      Code:
                      capture log close _all
                      
                      log using mylog1.txt, name(mylog1) text replace
                      di "This goes in mylog1 only"
                      
                      log using mylog2.txt, text replace
                      di "This goes in logs mylog1 and mylog2"
                      
                      log close mylog1
                      
                      di "Now that mylog1 is closed, this will go in mylog2 only"
                      
                      log close
                      I hope this helps

                      Comment


                      • #12
                        Thank you, Carlos, for the example. I tried something similar shortly after posting my question to test it for myself. Until now I thought Stata was only capable of generating one log file at a time.

                        Comment

                        Working...
                        X