Announcement

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

  • Converting sting into Stata Datetime

    Hello everyone,

    I'm having a (possibly) silly issue with the conversion of a string into a datetime. In particular, the string is displaying day-month-year hour:minute:second (02-Jan-2022 03:00:00) and I used the following commands to create a dateime


    gen datetime_3h = clock(standard_time, "DMYhms")
    format datetime_3h %tc

    which, however does not do it exactly as it generates, for the specific case, 02jan2022 03:00:28.

    I have attached the screenshot of the dataset with the newly-generated variable.

    Do you know how I can fix this and get 02jan2022 03:00:00 exactly?

    Thank you


    Click image for larger version

Name:	Screenshot 2025-01-29 at 08.05.46.png
Views:	1
Size:	372.8 KB
ID:	1771740

  • #2
    See

    Code:
    help datetime
    for the repeated advice that you must specify a double to hold such values. Every other detail was correct.

    Code:
    . clear
    
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . gen double datetime_3h = clock("02-Jan-2022 03:00:00", "DMYhms")
    
    . format datetime_3h %tc
    
    . l
    
         +--------------------+
         |        datetime_3h |
         |--------------------|
      1. | 02jan2022 03:00:00 |
         +--------------------+
    .

    Comment


    • #3
      Thank you so much. That worked perfectly

      Comment

      Working...
      X