Announcement

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

  • Convert string date in MM/DD/YYYY hh:mm:ss format to Stata date %tc (e.g.: 01jul2021 00:00:00)

    Hi everyone,

    A very quick question about date variables:

    I want to transform a string variable written as "MM/DD/YYYY hh:mm:ss"into a %tc format as the one mentionned in the title.
    The hours-minutes-seconds are always "00:00:00, 01:00:00, 02:00:00, 03:00:00, etc".

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str17 Date
    "07/01/21 00:00:00"
    "07/01/21 01:00:00"
    "07/01/21 02:00:00"
    "07/01/21 03:00:00"
    "07/01/21 04:00:00"
    "07/01/21 05:00:00"
    "07/01/21 06:00:00"
    "..."
    end
    I tried that but not worked:

    Code:
    gen times = date(Date, "MDYhhmmss")
    (4,416 missing values generated)
    Could anyone provide me a solution please? Thank you.

    Michael
    Last edited by Michael Duarte Goncalves; 02 Nov 2023, 09:55.

  • #2
    The only twist here is the need to spell out the century. In any case all is documented at help datetime

    Code:
    gen double wanted = clock(Date, "MD20Y hms")
    format wanted %tc

    Comment


    • #3
      Hi Nick Cox:

      Thank you for your help.
      I will have a look at help datetime.

      As usual, the code that you provide reflects what I need.

      Lovely afternoon.
      Michael

      Comment

      Working...
      X