Announcement

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

  • Basic date issue, 2 digit year and slashes

    Hi,

    How can I convert the following date format:

    03/22/94

    into a format similar to this:

    13dec1982

    Thanks

  • #2
    This is well documented. There is no road to understanding of dates and times in Stata that does not include a visit to

    Code:
    help datetime

    Code:
    . di daily("03/22/94", "MDY", 2025)
    12499
    
    . di %td daily("03/22/94", "MDY", 2025)
    22mar1994
    shows some technique with individual string values if that is your problem, but you are likely to need equivalent operations with generate and format commands.

    Alternatively, if you're showing us one display format for a numeric date and you need another, then

    Code:
    help datetime_display_formats
    is where to look for details.

    Comment


    • #3
      I have these dates and my code to covert them into a Stata date did not work.

      here i the date variable I have

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str8 var1
      "02/04/94"
      "03/22/94"
      "05/17/94"
      "07/06/94"
      "08/16/94"
      "09/27/94"
      "11/15/94"
      "12/20/94"
      "02/01/95"
      "03/28/95"
      "05/23/95"
      "07/06/95"
      "08/22/95"
      "09/26/95"
      "11/15/95"
      "12/19/95"
      "01/31/96"
      "03/26/96"
      "05/21/96"
      "07/03/96"
      "08/20/96"
      "09/24/96"
      "11/13/96"
      "12/17/96"
      "02/05/97"
      "03/25/97"
      "05/20/97"
      "07/02/97"
      "08/19/97"
      "09/30/97"
      "11/12/97"
      "12/16/97"
      "02/04/98"
      "03/31/98"
      "05/19/98"
      "07/01/98"
      "08/18/98"
      "09/29/98"
      "11/17/98"
      "12/22/98"
      "02/03/99"
      "03/30/99"
      "05/18/99"
      "06/30/99"
      "08/24/99"
      "10/05/99"
      "11/16/99"
      "12/21/99"
      "02/02/00"
      "03/21/00"
      "05/16/00"
      "06/28/00"
      "08/22/00"
      "10/03/00"
      "11/15/00"
      "12/19/00"
      "01/03/01"
      "01/31/01"
      "03/20/01"
      "04/18/01"
      "05/15/01"
      "06/27/01"
      "08/21/01"
      "09/17/01"
      "10/02/01"
      "11/06/01"
      "12/11/01"
      "01/30/02"
      "03/19/02"
      "05/07/02"
      "06/26/02"
      "08/13/02"
      "09/24/02"
      "11/06/02"
      "12/10/02"
      "01/29/03"
      "03/18/03"
      "05/06/03"
      "06/25/03"
      "08/12/03"
      "09/16/03"
      "10/28/03"
      "12/09/03"
      "01/28/04"
      "03/16/04"
      "05/04/04"
      "06/30/04"
      "08/10/04"
      "09/21/04"
      "11/10/04"
      "12/14/04"
      "02/02/05"
      "03/22/05"
      "05/03/05"
      "06/30/05"
      "08/09/05"
      "09/20/05"
      "11/01/05"
      "12/13/05"
      "01/31/06"
      end
      I tried

      Code:
      gen month = substr(var1, 1, 2)
      gen day = substr(var1, 4, 2)
      gen year = substr(var1, 7, 2)
      
      destring year month day, replace
      gen temp_year = real(year)
      replace temp_year = 2000 + temp_year if temp_year < 50
      replace temp_year = 1900 + temp_year if temp_year >= 50
      
      gen date = mdy(month, day, year)  
      format date %td
      But it did not work as I got error message of type mismatch

      Comment


      • #4
        As already hinted in #2

        Code:
        gen wanted = daily(var1, "MDY", 2025)
        format wanted %td
        gets you there directly and

        Code:
        quantile wanted
        would give a check for absurd results.

        I didn't try your code but the step-by-step logic is sound except that
        Code:
        gen temp_year = real(year) 
        in principle is redundant if the previous destring was successful; in practice it is illegal for the reason given. Stata squawks when asked to convert real to real; one might expect that to be indulged, but it is not, perhaps because trying to do that shows some confusion about your storage types.

        Comment

        Working...
        X