Announcement

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

  • Formatting datetime from one format to another in same variable

    Hello everyone!

    I am facing some issues with my timestamp variable. The data that I have received from the field are in 2 formats:

    1) 17-04-2022 21:15
    2) 2022-04-04:09:00:00.PM

    I want to bring all the observations in the variable into a standard format.

    Could someone please suggest some ways to go about the same? Thanks for your help!

  • #2
    Code:
    clear 
    input str42 problem 
    "17-04-2022 21:15"
    "2022-04-04:09:00:00.PM"
    end 
    
    gen double solution = clock(problem, "DMY hm")
    replace solution = clock(problem, "YMD hms") if missing(solution)
    
    format solution %tc 
    
    list 
    
         +---------------------------------------------+
         |                problem             solution |
         |---------------------------------------------|
      1. |       17-04-2022 21:15   17apr2022 21:15:00 |
      2. | 2022-04-04:09:00:00.PM   04apr2022 21:00:00 |
         +---------------------------------------------+
    
    .

    Comment

    Working...
    X