Announcement

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

  • Creating local (or global) macro of current date, in DDNNCCYY format

    Hi,
    I'm trying to automatically create a global macro in my do files of the current date, that I can append to log file names etc.
    In format DDNNYYYY. So today would be 02112018.

    I was hoping to be able to do this in one or two lines with macro functions.
    But the closest I can get is as follows..which involves creating a variable.
    Any suggestions for a better way?

    sysuse auto
    gen date_current_temp = date("`c(current_date)'","DMY",2000) in 1
    format date_current_temp %tdDDNNCCYY
    global date = string(date_current_temp,"%tdDDNNCCYY") in 1
    drop date_current_temp
    di "$date"

    Regards,

    Andrew

  • #2
    I recommend avoiding global macro variables as much as possible. If you're creating the log file from within a do-file, then you can work with a local macro. Something like the following:
    Code:
    local today : display %tdCYND date(c(current_date), "DMY")
    display in smcl as text "`today'"
    log using my_log`today'.smcl name(log) nomsg
    // etc.

    Comment


    • #3
      Code:
      local wanted : di %tdDNCY daily("$S_DATE", "DMY")
      
      di "`wanted'"
      02112018

      Comment


      • #4
        Hi Joseph and Nick.
        Thanks heaps. Works great.
        Regards,
        Andrew

        Comment


        • #5
          This FAQ discusses the issue too

          https://www.stata.com/support/faqs/d...date-and-time/

          Comment


          • #6
            Hi! Is there a way of doing this for datetime (that is, including the exact time as well) instead of date?? Thanks!

            Comment


            • #7
              Code:
              . di "$S_DATE $S_TIME"
              13 Jun 2020 16:55:59

              Comment

              Working...
              X