Announcement

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

  • Destring a Date

    How do I destring a date? destring Date_of_death, replace is not working. The dat now reads like this: 12/09/21
    What am I doing incorrectly.

    Thank you in advance for help.

  • #2
    See

    Code:
    help datetime
    Code:
    gen wanted= daily(Date_of_death, "DMY", 2050)
    format wanted %td
    It is unclear whether your dates are MDY or DMY. Change appropriately.

    Comment


    • #3
      Andrew Musau gave a correct positive answer!

      Let's be clear. destring applied to dates will usually not help. You could remove the slashes and end with numbers like 120921 but that's still not a Stata date and you would still need to convert it.

      I can think of one (and only one) case in which destring for dates is the right idea: you have calendar years that are strings such as "2021"

      There is, for once, a bad example in the manual entry for destring in which spaces are removed from daily dates. That really doesn't help. As I am credited (correctly) as being the original author of destring, it is perhaps graceless to point this out. and to underline that I didn't add that example to the documentation. I have flagged this to StataCorp.

      There is better advice at https://www.stata-journal.com/articl...article=dm0098 (recently emerged from behind a paywall). See especially Sections 2.2 and 3.




      Comment


      • #4
        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment

        Working...
        X