Announcement

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

  • Converting 4 -digit year to 2 digit year in stata

    Hi

    I am trying to format my data that looks like 1/28/2014 to a date that looks like this 1/28/14 (that represents January 28, 2014)

    I tried using format %tdnn/dd/yy but that gives me something different.

    I just want to take the four digit year and turn it into a two digit year.

    Thanks!

  • #2
    Ava,

    What do you mean when you say "that gives me something different"? What you have done (format %tdnn/dd/yy) is probably about as close as you can get. Keep in mind that a Stata date is just the number of days since January 1, 1960. The year is not stored separately such that you can control how many digits are stored. A Stata data can be formatted in a multitude of different ways, such as the one you propose. This does not change the way the data is stored, just the way it is displayed.

    If you truly want to store the year as two digits instead of four digits, you would have to save the date as a string using the string() or strofreal() functions:

    Code:
    gen newdate=string(olddate,"%tdnn/dd/yy")
    Regards,
    Joe

    Comment


    • #3
      To echo Joe's comments:

      Changing a display format only changes what is displayed. It has precisely no effect on what is stored. I can see no reason why you would want to store a two-digit year. Naturally I understand that you want to see a two-digit year.

      Code:
      . di %tdnn/dd/yy mdy(8,24,2015)
       8/24/15
      
      . di  mdy(8,24,2015)
      20324
      
      . di %td   mdy(8,24,2015)
      24aug2015

      Comment

      Working...
      X