Announcement

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

  • Extracting year from date

    Dear Community,

    I want to extract a year from a date but can't seem to find a working answer. If I copy the date it looks like this: 01aug2003 00:00:00

    Below is a dataex of the variable:

    1.3753152e+12
    1.3753152e+12
    1.3753152e+12
    1.3753152e+12
    1.3753152e+12
    1.3753152e+12
    1.3753152e+12
    1.2334464e+12
    1.2334464e+12
    1.2334464e+12
    1.2334464e+12
    1.2334464e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.5566688e+12
    1.8893952e+12
    1.8893952e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.230768e+12
    1.901232e+12
    1.901232e+12
    1.335312e+12
    1.335312e+12
    1.335312e+12
    1.335312e+12
    1.335312e+12
    1.5304896e+12
    1.5304896e+12
    1.6102368e+12
    1.6298496e+12
    1.6298496e+12
    1.6298496e+12
    1.723248e+12
    1.723248e+12
    1.723248e+12
    1.723248e+12
    1.723248e+12
    1.723248e+12
    1.723248e+12
    1.2845952e+12
    1.2845952e+12
    1.4543712e+12
    1.4543712e+12
    1.4543712e+12
    1.4543712e+12
    1.4543712e+12
    1.606608e+12
    1.606608e+12
    1.606608e+12
    1.606608e+12
    1.606608e+12
    1.606608e+12
    1.606608e+12
    1.291248e+12
    1.291248e+12
    1.291248e+12
    1.291248e+12
    1.5147648e+12
    1.5147648e+12
    1.5147648e+12
    1.5147648e+12
    1.5147648e+12
    1.5147648e+12
    1.5147648e+12
    1.719792e+12
    1.719792e+12
    1.719792e+12
    1.719792e+12
    1.719792e+12
    1.719792e+12
    1.719792e+12
    1.369008e+12
    1.369008e+12
    1.369008e+12
    1.369008e+12
    1.369008e+12
    1.369008e+12
    1.369008e+12
    end
    format %tc becameceo
    [/CODE]

    Any help would be appreciated!

  • #2
    See

    Code:
    help datetime
    The time of day information here is spurious. Daily dates would be more useful. Here's how to get years too. You can do both with year(dofc())

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double becameceo
    1.3753152e+12
    1.2334464e+12
    1.5566688e+12
    1.8893952e+12
     1.230768e+12
     1.901232e+12
     1.335312e+12
    1.5304896e+12
    1.6102368e+12
    1.6298496e+12
     1.723248e+12
    1.2845952e+12
    1.4543712e+12
     1.606608e+12
     1.291248e+12
    1.5147648e+12
     1.719792e+12
     1.369008e+12
    end
    format %tc becameceo
    
    sort became 
    gen date = dofc(becameceo)
    format date %td 
    
    gen year = year(date)
    
    list 
    
         +---------------------------------------+
         |          becameceo        date   year |
         |---------------------------------------|
      1. | 01jan1999 00:00:00   01jan1999   1999 |
      2. | 01feb1999 00:00:00   01feb1999   1999 |
      3. | 15sep2000 00:00:00   15sep2000   2000 |
      4. | 01dec2000 00:00:00   01dec2000   2000 |
      5. | 25apr2002 00:00:00   25apr2002   2002 |
         |---------------------------------------|
      6. | 20may2003 00:00:00   20may2003   2003 |
      7. | 01aug2003 00:00:00   01aug2003   2003 |
      8. | 01feb2006 00:00:00   01feb2006   2006 |
      9. | 01jan2008 00:00:00   01jan2008   2008 |
     10. | 01jul2008 00:00:00   01jul2008   2008 |
         |---------------------------------------|
     11. | 30apr2009 00:00:00   30apr2009   2009 |
     12. | 29nov2010 00:00:00   29nov2010   2010 |
     13. | 10jan2011 00:00:00   10jan2011   2011 |
     14. | 25aug2011 00:00:00   25aug2011   2011 |
     15. | 01jul2014 00:00:00   01jul2014   2014 |
         |---------------------------------------|
     16. | 10aug2014 00:00:00   10aug2014   2014 |
     17. | 15nov2019 00:00:00   15nov2019   2019 |
     18. | 31mar2020 00:00:00   31mar2020   2020 |
         +---------------------------------------+
    Code:
    
    

    Comment


    • #3
      Dear Nick,

      I appreciate your help! It all works now.

      Comment


      • #4
        Good, but as you're new here let me flag a key detail from the FAQ Advice.

        You're asked to explain exactly what you did and exactly what didn't work.

        That way, it is, we hope. easier to explain why something didn't work, and ideally you gain some understanding of why not. And all of that is visible to any future readers who will be attracted by the thread title and presumably struggling for the same or similar reasons.

        Comment

        Working...
        X