Announcement

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

  • Dates

    I have a string variable containing the following type of data "20apr2022 17:49:04" I want to get rid of the hour, minutes and seconds and create a day variable, but it seems not possible to extract d, month and year

  • #2
    Code:
    clear*
    set obs 1
    gen str_date = "20apr2022 17:49:04"
    
    gen date = daily(str_date, "DMY#")
    format date %td
    
    gen year = year(date)
    gen month = month(date)
    gen day = day(date)
    
    list, noobs clean
    For most purposes you will not need the separate year, month, and day variables. So unless you know you will require them for a specific purpose, I advise not cluttering up your data set with them. The variable date, itself, has all that information, and anything you might want to do with that information can be done with Stata datetime functions.

    Comment

    Working...
    X