Announcement

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

  • Problem working with dates

    Hello, I am using the following code to convert string dates to numeric:

    capture drop dos_rc //making into usable date
    gen dos_rc = date(dos, "DM20Yhm")
    format dos_rc %td

    Some dates are converted and some are not despite the string having the same format for those converted and not converted. doc_rc is the recoded date variable. Any ideas on why the code returns missing for some but not other cases?

    dos_rc dos
    05sep2017 5/9/2017 4:00 PM
    05sep2017 5/9/2017 4:00 PM
    05sep2017 5/9/2017 4:00 PM
    05sep2017 5/9/2017 4:00 PM
    05sep2017 5/9/2017 4:00 PM
    5/15/2017 3:30 PM
    5/15/2017 2:00 PM
    5/15/2017 2:00 PM
    5/15/2017 2:00 PM


  • #2
    When Stata enconters 5/15/2017 3:30 PM, etc., and tries to parse it against "DM20Yhm" it founders because there is no 15th month in the year. So either 5/15/2017 is a data error, or you have surreptitiously switched to "MD20Yhm" format in the dos variable and intend this to represent 15 May 2017.

    Anyway, you can't mix different string date variable formats in one variable and convert them in one command. At best you can do it with a series of commands that handles each format separately. But then you'd better be sure exactly which format a date like 1/2/2017 represents: is it 2 Jan or is it Feb 1? Stata won't be able to guess and your code will have to somehow distinguish those possibilities with -if/in- conditions.

    Comment


    • #3
      I see the mistake now. Thanks very much!

      Comment

      Working...
      X