Announcement

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

  • Date format works only with display command

    My goal is to create a folder with the date formatted in the way I like.

    I found the specific format I want but it works only with the display command.

    This code
    Code:
     display %td_CCYYNNDD date(c(current_date), "DMY")
    Produces the result I want
    20181120
    (which is the 20th of November 2018)

    But I was not able to store this string of digits as a scalar/tempvar/local and use it anywhere else

    Scalar -
    Code:
    scalar a = %td_CCYYNNDD date(c(current_date), "DMY")
    or local macro -
    Code:
    local D = %td_CCYYNNDD date(c(current_date), "DMY")
    Both produce the same error
    %td_CCYYNNDD invalid name
    Last edited by Mor Zahavi; 20 Nov 2018, 07:59.
    Stata/MP 15.1

  • #2
    Your title is incorrect. A date display format can work fine with scalar or local assignments. The problem is that your expression is not a legal expression in either case.

    Code:
    . local D : display %td_CCYYNNDD date(c(current_date), "DMY")
    
    . di "`D'"
     20181120
    
    . local D2 = string(date(c(current_date), "DMY"), "%td_CCYYNNDD")
    
    . di "`D2'"
    20181120
    
    
    . scalar D = string(date(c(current_date), "DMY"), "%td_CCYYNNDD")
    
    . di scalar(D)
    20181120

    Comment


    • #3
      Excellent. Could you please refer me to the part of the manual which explains when to write in a macro
      Code:
       
       local D :
      or
      Code:
        
       local D =
      Stata/MP 15.1

      Comment


      • #4
        I know this, so I learned it somewhere. There isn't always just one place that explains each point or if there is I don't guarantee to remember where it is.

        You need = when you want the result of evaluating an expression, but you need : for display directives.

        Code:
        help macro 
        mentions the last and and links to

        Code:
        help extended fcn

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Your title is incorrect. A date display format can work fine with scalar or local assignments. The problem is that your expression is not a legal expression in either case.

          Code:
          . local D : display %td_CCYYNNDD date(c(current_date), "DMY")
          
          . di "`D'"
          20181120
          
          . local D2 = string(date(c(current_date), "DMY"), "%td_CCYYNNDD")
          
          . di "`D2'"
          20181120
          
          
          . scalar D = string(date(c(current_date), "DMY"), "%td_CCYYNNDD")
          
          . di scalar(D)
          20181120
          and one more thing - can you also show me how to add the time in 24 hours format and put an underscore between the date and time (for example - 20181120_1825)
          Stata/MP 15.1

          Comment


          • #6
            Hi,

            Originally posted by Mor Zahavi View Post

            and one more thing - can you also show me how to add the time in 24 hours format and put an underscore between the date and time (for example - 20181120_1825)
            you can do so in the very same way. You need to change the datetime information you're working with from daily to clock base, however:

            Code:
            . display %tc_CCYYNNDD!_HHMM clock(c(current_date)+c(current_time),"DMYhms")
             20181120_1752
            The "!" in the display format is an escape character for the underscore character which, if not escaped, stands for a space character in the resulting string.

            Regards
            Bela
            Last edited by Daniel Bela; 20 Nov 2018, 09:56.

            Comment


            • #7
              Originally posted by Daniel Bela View Post
              Hi,



              you can do so in the very same way. You need to change the datetime information you're working with from daily to clock base, however:

              Code:
              . display %tc_CCYYNNDD!_HHMM clock(c(current_date)+c(current_time),"DMYhms")
              20181120_1752
              The "!" in the display format is an escape character for the underscore character which, if not escaped, stands for a space character in the resulting string.

              Regards
              Bela
              Very Helpful. Thanks Daniel.
              Stata/MP 15.1

              Comment

              Working...
              X