Announcement

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

  • Extracting year from a date

    Hello,

    First, sorry if this question has already been solved. I researched it but I did not found it.

    I would like to pick up the year of a date. This is an example of what I have:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 dateintr
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "2-Jul-03" 
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "23-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "12-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "7-Jul-03" 
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "17-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "17-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "22-Jun-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "17-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "18-Jul-03"
    "19-Jul-03"
    "19-Jul-03"
    "19-Jul-03"
    "19-Jul-03"
    end

    After looking around, I think one of the possible solutions is to use date() function, but it is not working as it just gives missing:

    Code:
    gen year=date(dateintr, "DMY")
    Any ideas?

  • #2
    The reason your solution is not working is that with strings like "19-Jul-03" Stata has no way of knowing whether the 03 refers to 2003, or 1903, or 1803, or... To get the -date()- function to work with these strings, you will have to tell Stata. Assuming all of your dates are in year 2000 or later:
    Code:
    gen date = date(dateintr, "DM20Y")
    format date %td
    gen year = year(date)

    Comment


    • #3
      Thanks a lot Clyde Schechter . It works really well.

      Comment

      Working...
      X