Announcement

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

  • New to stata

    I am new to Stata and have been to the "essentials" training. I have a dataset with one var as dates in string format ( str8 type "12/15/23"). I am trying to create a new_date var with command -- generate new_date = date(date_old, "MDY"). When I enter this command I get "542 missing values generated" (there are 542 obs). Does the same thing if I use "daily" instead of "date". What am I doing wrong?

  • #2
    Assuming that you want this century, try
    Code:
    generate int new_date = date(date_old, "MDY", 2050)
    It's covered in the help file.

    Comment


    • #3
      Thanks. That did the trick. I didn't realize that about the 2 digit year and century.

      Comment


      • #4
        Joseph Coveney's advice is slightly misstated here. It is correct in concept: the problem is that the date fails to say which century it belongs to, so Stata cannot arrive at an answer. But using -date(date_old, "MDY", 2050)- will give dates that range between 1951 and 2050, not the current century. If you want the range of years to be the 2000's, use -date(date_old, "MD20Y")-.

        Code:
        . display %td daily("1/15/55", "MDY")
                .
        
        .
        . display %td daily("1/15/55", "MDY", 2050) // MIGHT BE USEFUL FOR BIRTH DATES OF CURRENTLY LIVING PEOPLE
        15jan1955
        
        .
        . display %td daily("1/15/55", "MD20Y") // MIGHT BE USEFUL FOR DATA INCLUDING FUTURE PROJECTIONS THROUGH 2099
        15jan2055
        And, putting on my pedant hat, if you really wanted the current century, which is, technically, the years 2001-2100, you would use -daily("1/15/55", "MDY", 2100)-. And, no, I can't think of a good use case for this off hand.
        Last edited by Clyde Schechter; 12 Apr 2025, 18:07.

        Comment

        Working...
        X