Announcement

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

  • convert string to date: some observations are missing values

    Dear Stata Users

    I am converting string to date variable running the following code:

    Code:
    gen date2 = date(date, "MDY")
    format date2 %td

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 date float date2
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/12/2011" 18962
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    "01/13/2011"     .
    end
    ------------------ copy up to and including the previous line ------------------

    Stata, however, converts some to dates but generates missing value for the others.

    Any idea of how to fix it?

    Thanks.
    Last edited by Olena Onishchenko; 09 Nov 2021, 13:42.

  • #2
    I cannot replicate your issue. Perhaps you specified "DMY" instead of "MDY".

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 date
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/12/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    "01/13/2011"
    end
    
    gen date2 = date(date, "MDY")
    format date2 %td
    Res.:

    Code:
    . l, sep(0)
    
         +------------------------+
         |       date       date2 |
         |------------------------|
      1. | 01/12/2011   12jan2011 |
      2. | 01/12/2011   12jan2011 |
      3. | 01/12/2011   12jan2011 |
      4. | 01/12/2011   12jan2011 |
      5. | 01/12/2011   12jan2011 |
      6. | 01/12/2011   12jan2011 |
      7. | 01/12/2011   12jan2011 |
      8. | 01/12/2011   12jan2011 |
      9. | 01/12/2011   12jan2011 |
     10. | 01/12/2011   12jan2011 |
     11. | 01/13/2011   13jan2011 |
     12. | 01/13/2011   13jan2011 |
     13. | 01/13/2011   13jan2011 |
     14. | 01/13/2011   13jan2011 |
     15. | 01/13/2011   13jan2011 |
     16. | 01/13/2011   13jan2011 |
     17. | 01/13/2011   13jan2011 |
     18. | 01/13/2011   13jan2011 |
         +------------------------+
    Last edited by Andrew Musau; 09 Nov 2021, 14:04.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      I cannot replicate your issue. Perhaps you specified "DMY" instead of "MDY".
      Most certainly so, as we can confirm this from the observations where the calculation has succeeded:
      Code:
      . di date("01/12/2011","MDY")
      18639
      
      . di date("01/12/2011","DMY")
      18962

      Comment

      Working...
      X