Announcement

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

  • Extracting the date, month and year from a variable

    Dear all

    How can I extract the date, month and year from a date variable in STATA

    Below is an example of my dataset

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 Q2_9a
    "2018-05-07"
    "2011-03-07"
    "2018-10-15"
    "1900-06-01"
    "2018-01-01"
    "2014-05-13"
    "2014-10-23"
    "2017-05-16"
    "2018-01-01"
    "2014-05-12"
    ""          
    "2018-01-01"
    "2014-05-14"
    ""          
    "2013-10-03"
    "2016-10-27"
    "2008-04-03"
    "2014-05-13"
    "2015-12-24"
    "2017-09-14"
    "2012-05-07"
    "2014-07-09"
    "2011-03-01"
    "2016-09-01"
    "2018-01-01"
    "2018-05-04"
    "2014-08-01"
    "2010-10-11"
    "2018-05-07"
    "2018-04-16"
    "1993-04-27"
    "2015-05-07"
    "2012-04-16"
    "2018-05-14"
    "2018-05-11"
    "2016-05-07"
    "2015-08-04"
    "2005-11-01"
    "2017-10-06"
    "1900-06-01"
    "2017-06-12"
    "2011-05-25"
    "2015-05-07"
    "2014-12-10"
    "2014-10-23"
    "2016-12-02"
    "1900-06-01"
    "2008-01-10"
    "2005-08-31"
    "2005-05-17"
    "2006-12-31"
    "2010-10-12"
    "2000-10-16"
    "2011-07-01"
    ""          
    "2005-06-01"
    "2016-01-19"
    "1900-06-01"
    "2016-05-19"
    "2015-05-07"
    "2015-05-07"
    "2008-06-30"
    "1998-07-07"
    "1985-01-04"
    ""          
    ""          
    "2004-03-23"
    "2008-06-30"
    "2018-02-05"
    "2014-12-04"
    "2014-08-19"
    "2003-10-15"
    "1999-07-24"
    "2015-05-07"
    "2015-05-29"
    "2015-05-05"
    "2015-05-05"
    "2014-06-20"
    "2015-05-05"
    "2001-12-21"
    "2009-09-09"
    "2015-08-05"
    "2018-03-19"
    "2017-02-13"
    "2011-07-15"
    "2018-03-19"
    "1985-01-01"
    "2007-01-01"
    "2015-05-04"
    "1900-06-01"
    "2015-01-13"
    ""          
    "1981-01-01"
    "2015-05-06"
    "2017-03-31"
    "2017-05-01"
    "2015-11-04"
    "2016-01-01"
    "2015-06-25"
    "2016-08-25"
    end

    Thank you

  • #2
    See the help file for -split-. You’ll want something like:
    Code:
    split Q2_9a, parse("-") gen(part)
    rename part1 year
    rename part2 month
    rename part3 date
    Last edited by Carole J. Wilson; 14 Oct 2018, 22:07. Reason: Fixed quotes
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      It's not actually a date variable in Stata. It's a string variable in Stata.

      For your case, you'd use the substr() function. Or split.
      Code:
      split Q2_9a, generate(d) parse(-)
      rename d1 year
      rename d2 month
      rename d3 day
      If it were a Stata date, you could
      Code:
      help datetime
      for the functions to extract the components.

      Comment


      • #4
        If you use split you will need the destring option to make these results easier to use. Otherwise use daily() as here.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str10 Q2_9a
        "2018-05-07"
        "2011-03-07"
        "2018-10-15"
        "1900-06-01"
        "2018-01-01"
        "2014-05-13"
        "2014-10-23"
        "2017-05-16"
        "2018-01-01"
        "2014-05-12"
        end 
        
        split Q2_9a, parse(-) destring 
        
        rename (Q2_9a?) (year month day)
        
        list 
        
             +---------------------------------+
             |      Q2_9a   year   month   day |
             |---------------------------------|
          1. | 2018-05-07   2018       5     7 |
          2. | 2011-03-07   2011       3     7 |
          3. | 2018-10-15   2018      10    15 |
          4. | 1900-06-01   1900       6     1 |
          5. | 2018-01-01   2018       1     1 |
             |---------------------------------|
          6. | 2014-05-13   2014       5    13 |
          7. | 2014-10-23   2014      10    23 |
          8. | 2017-05-16   2017       5    16 |
          9. | 2018-01-01   2018       1     1 |
         10. | 2014-05-12   2014       5    12 |
             +---------------------------------+
        
        gen ddate = daily(Q2_9a, "YMD")
        
        format ddate %td
        
        list 
        
             +---------------------------------------------+
             |      Q2_9a   year   month   day       ddate |
             |---------------------------------------------|
          1. | 2018-05-07   2018       5     7   07may2018 |
          2. | 2011-03-07   2011       3     7   07mar2011 |
          3. | 2018-10-15   2018      10    15   15oct2018 |
          4. | 1900-06-01   1900       6     1   01jun1900 |
          5. | 2018-01-01   2018       1     1   01jan2018 |
             |---------------------------------------------|
          6. | 2014-05-13   2014       5    13   13may2014 |
          7. | 2014-10-23   2014      10    23   23oct2014 |
          8. | 2017-05-16   2017       5    16   16may2017 |
          9. | 2018-01-01   2018       1     1   01jan2018 |
         10. | 2014-05-12   2014       5    12   12may2014 |
             +---------------------------------------------+

        Comment

        Working...
        X