Announcement

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

  • Converting a string variable containing a date into a %tc variable

    Dear users,

    I am utterly confused. I have a string variable "date" (example observation in "date" is "7jul2013 0:00:00"). When I convert the string into a new clocktime variable using:

    gen datedate = clock(date, "DMYhms")
    format datedate %tc

    Stata returns: "06jul2013 23:59:37"

    When I do:
    gen datedate = clock(date, "DMYhms")
    format datedate %tC

    Stata returns: "06jul2013 23:59:12"

    When I do:
    gen datedate = Clock(date, "DMYhms")
    format datedate %tc

    Stata returns: "06jul2013 23:59:37" (like the "clock" function)

    And when I do:
    gen datedate = Clock(date, "DMYhms")
    format datedate %tC

    Stata returns: "06jul2013 23:59:12" again


    What should I do to obtain the correct date and time. i.e., "7jul2013 0:00:00" as a %tc variable (instead of a string)?

  • #2
    See remarks in

    Code:
    help datetime 
    such as

    Code:
      SIF type        HRF to SIF                     Note
            ---------------------------------------------------------------------
            datetime/c      tc =      clock(HRFstr, mask)  tc must be double
            datetime/C      tC =      Clock(HRFstr, mask)  tC must be double
    Warning: To prevent loss of precision, datetime SIFs must be stored as doubles.

    1. You have datetimes stored in the string variable mystr, an example being "2010.07.12 14:32". To
    convert to SIF datetime/c, you type

    . gen double eventtime = clock(mystr, "YMDhm")

    The mask "YMDhm" specifies the order of the datetime components. In this case, they are year,
    month, day, hour, and minute.
    P.S. A very long line of Stata users missed this point before you. But the advice remains: Do please read the help carefully.
    Last edited by Nick Cox; 08 Feb 2018, 03:31.

    Comment


    • #3
      Oh man... How could I overlook that?

      Thanks a lot Nick!

      Comment

      Working...
      X