Announcement

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

  • Extracting Date from DateTime Format

    I have dates in the format: 12FEB1993:00:00:00.000 and would like to have a variable with just 12FEB1993 and, ultimately, 1993Q1. The step I am struggling with is just getting the date extracted from the DateTime value. I've read the help documentation and various discussions on this site but, when I use the suggested syntax (i.e. gen double time = monthly(date_start, "MDY"), the variable populates as all missing values.

    . gen right = monthly(date_start, "MDY")
    (310,401 missing values generated)


    I'm thinking that there is a chance that Stata isn't reading the original string as a date variable or there is some step that should be conducted prior that I have been missing. Any help would be much appreciated.


  • #2
    You don't have and don't want monthly dates, so monthly() is a hockey player at a ballet class. I don't know where that's suggested for what you are doing. Nor is the example that you give MDY at all, as the order of elements is different to start with and you have a time of day too.

    I generally fire up display with concrete examples to check my ideas on what to do.

    Code:
    . di %td daily("12FEB1993:00:00:00.000", "DMY hms")
    12feb1993
    
    . di %tq qofd(daily("12FEB1993:00:00:00.000", "DMY hms"))
    1993q1
    So, push your date-times through daily() to get daily dates and those through qofd() to get quarterly dates.

    Comment


    • #3
      Sir Nick,

      What if the datetime format is YMD followed by time? If I just want a MY or YM date, would I use

      "gen monthlydate = mofd(datetime)" ?

      I try this, but I get a type mismatch. My ultimate goal is to collapse and aggregate data by month.

      Thank you for your guidance!
      Last edited by josh scott; 21 Jul 2021, 12:55.

      Comment

      Working...
      X