Announcement

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

  • How to combine year,month,day to year-month-day?

    Hi,
    I'd like to use year, month, day to generate a new variable datenew (showing below).

    id year month day datenew
    1 2010 8 23 2010-8-23
    2 2019 9 1 2019-9-1
    Could anyone help with this? Thank you in advance!
    Best,
    Z

  • #2
    Code:
    gen datenew=mdy(month,day,year)
    format datenew %tdCCYY-NN-DD
    
    . list
    
         +--------------------------------------+
         | id   year   month   day      datenew |
         |--------------------------------------|
      1. |  1   2010       8    23   2010-08-23 |
      2. |  2   2019       9     1   2019-09-01 |
         +--------------------------------------+
    Would recommend reading the contents of -help datetime-, which describe in detail how to do this and many other procedures to do with Stata's datetime format.

    Comment


    • #3
      Originally posted by Ali Atia View Post
      Code:
      gen datenew=mdy(month,day,year)
      format datenew %tdCCYY-NN-DD
      
      . list
      
      +--------------------------------------+
      | id year month day datenew |
      |--------------------------------------|
      1. | 1 2010 8 23 2010-08-23 |
      2. | 2 2019 9 1 2019-09-01 |
      +--------------------------------------+
      Would recommend reading the contents of -help datetime-, which describe in detail how to do this and many other procedures to do with Stata's datetime format.
      Thank you Ali, this is exactly what I want!

      Comment

      Working...
      X