Announcement

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

  • String to date variable with time when string does not follow the format DMY ## or DMYhms

    Hi all,

    I have a large set of string variables (too large to encode) that contain dates and specific times in the following manner:
    09SEP19:09:00:00
    I would like to convert it to dates using either
    gen new_date_var = date(string_var, "DMY###")
    gen new_date_var = clock(string_var, "DMY#hms")

    However none of these work apparently because of the : between the Year and the Time (---19:09---) and therefore the new_date_var generates only missing values.
    I also tried to use multiple # to account for the additional : but it is not working.

    I would highly appreciate any help =)

    Thank you for your time!

  • #2
    Code:
    clear
    set obs 1
    gen string_var = "09SEP19:09:00:00"
    gen double new_var = clock(string_var, "DM20Yhms")
    format new_var %tc
    works for me, on the assumption that your intended year is 2019, not 1919 or in some other century.

    Important: when creating clock variables it is crucial to create the variable as a double. Otherwise your times will be inaccurate because the default float data storage type is not large enough to hold all the necessary digits.

    Comment


    • #3
      Dear Clyde,

      thank you so much it worked! I was missing the double then =)
      Have a nice day!

      Comment


      • #4
        You were missing the century too.

        Comment

        Working...
        X