Announcement

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

  • Date challenges in STATA 15

    Hello,

    I need help troubleshooting a date conversion.

    I have date variables imported from .csv that appear as mm/dd/yy (format %9s, type str8). I am trying to convert to STATA dates and then format %td.

    data:

    date
    4/26/19
    4/10/19
    5/1/19
    4/11/19
    8/27/19
    4/23/19

    the command

    generate date_new=date(date,"MDY")

    returns all missing values

    This has worked for me before. Please help. What am I missing?

  • #2
    Code:
    MD20Y
    — where letters should be upper case even if the forum software flips them to lower.
    Last edited by Nick Cox; 22 Jun 2023, 11:29.

    Comment


    • #3
      The dates you are inputting do not have a complete four-digit year. Does 19 mean 2019? 1919? 519? Something else? Stata has no way to know.

      You must supply the century information. If all of these values are 21st century, you can do that as:
      Code:
      gen date_new = date(date, "MD20Y")
      If some of your years are in the 21st century but some go back to the 20th, then you have to specify a maximum year. For example, on the assumption that none of your dates are later than 2023 (but some might be as late as 2023):
      Code:
      gen date_new = date(date, "MDY", 2023)
      format date_new %td
      This will translate 1/1/23 as 1jan2023. It will translate 1/1/25 as 1jan1925. Etc.

      Added: Crossed with #2.

      Comment


      • #4
        Thank you, both. This worked. I do have a mix of 20th and 21st century dates, so needed this (gen date_new = date(date, "MDY", 2023)) code.
        Very much appreciated!

        Comment

        Working...
        X