Announcement

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

  • How to destring date/time with comma

    Hello,

    I have a date/time variable formatted in the following way: "01-24-2019, 03:26 PM" and cannot figure out how to destring it. I have tried several variations of gen date=clock(datetime,"MM-DD-YYYY, hm") but this hasn't worked. Any help would be much appreciated.

    Thank you,

    Sarah


  • #2
    The problem isn't the original string variable. You just don't quite have the syntax right for clock. You just need to specify the order of the elements, not try to match the layout of the string. Try this:
    Code:
    gen double date=clock("01-24-2019, 03:26 PM", "MDYhm")
    format %tc date

    Comment


    • #3
      I will just emphasize one thing in Sarah Edgington's response that she glossed over: the -double- after -gen- is absolutely crucial here. If you omit that, you will get precision errors.

      Comment


      • #4
        Thank you both. The problem I'm having with the syntax Sarah provided is changing the first line to apply to all of the dates and times, not just 1/24. Can you help?

        Comment


        • #5
          Just replace the "01-24-2019, 03:26 PM" with the name of the variable, leaving everything else exactly the same.

          Comment

          Working...
          X