Announcement

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

  • Convert date into years

    Hi, Stata users!
    I want to convert data time into years, in order to determine age.
    An example of my data is
    year yearborn
    2000 196010
    Any help would be appreciated.

  • #2
    Your example does not tell us how these variables are set up in Stata. In the future, please use the -dataex- command to post example data so that we can get an exact replica of your data. (-ssc install dataex-, -help dataex- for instructions on using it.)

    So you first have to convert yearborn to a Stata internal format date, and then you have to extract the year from it. I assume that 196010 refers to October 1960.

    Code:
    numdate month_year_of_birth = yearborn, pattern("YM")
    gen year_of_birth = year(month_year_of_birth)

    Comment


    • #3
      Thank you, Clyde Schechter for your answer. I just had now availability to try it.
      I tried your code
      numdate month_year_of_birth = yearborn, pattern("YM") but Stata replied to me "month_year_of_birth unrecognised kind of date-time".
      I also tried
      numdate yearly month_year_of_birth = yearborn, pattern("YM")
      but I just obtain missing values.
      Unfortunately I cannot use dataex because the computer that I am utilising cannot have internet connection.

      Comment


      • #4
        Hello Marta,

        Please check whether these commands apply to your needs.

        Code:
        . set obs 4
        number of observations (_N) was 0, now 4
        
        . input year yearbirth
        
                  year  yearbirth
          1. 2000 196010
          2. 2005 196112
          3. 2013 196103
          4. 2004 196209
        
        . tostring yearbirth, replace
        yearbirth was float now str6
        
        . gen yearborn = date(yearbirth, "YM")
        
        . format yearborn %td
        
        . list
        
             +-----------------------------+
             | year   yearbi~h    yearborn |
             |-----------------------------|
          1. | 2000     196010   01oct1960 |
          2. | 2005     196112   01dec1961 |
          3. | 2013     196103   01mar1961 |
          4. | 2004     196209   01sep1962 |
             +-----------------------------+
        
        . gen yearborn2 = year(yearborn)
        
        . list
        
             +----------------------------------------+
             | year   yearbi~h    yearborn   yearbo~2 |
             |----------------------------------------|
          1. | 2000     196010   01oct1960       1960 |
          2. | 2005     196112   01dec1961       1961 |
          3. | 2013     196103   01mar1961       1961 |
          4. | 2004     196209   01sep1962       1962 |
             +----------------------------------------+
        
        . gen age = year - yearborn2
        
        . list
        
             +----------------------------------------------+
             | year   yearbi~h    yearborn   yearbo~2   age |
             |----------------------------------------------|
          1. | 2000     196010   01oct1960       1960    40 |
          2. | 2005     196112   01dec1961       1961    44 |
          3. | 2013     196103   01mar1961       1961    52 |
          4. | 2004     196209   01sep1962       1962    42 |
             +----------------------------------------------+
        Best,

        Marcos
        Best regards,

        Marcos

        Comment


        • #5
          Note that Clyde had a typo in #2

          Code:
           numdate month_year_of_birth = yearborn, pattern("YM")
          should be
          Code:
            
           numdate month month_year_of_birth = yearborn, pattern("YM")

          Comment


          • #6
            Note that if the year and month are in a numeric variable with values like 196010 then

            Code:
            gen byear = floor(yearborn/100)
            is a direct one-liner.

            Otherwise, Marcos's example helpfully lays out step-by-step logic. It could also be telescoped:

            Code:
            clear 
            input year yearbirth
             2000 196010
             2005 196112
             2013 196103
             2004 196209
            end 
            gen yearborn = year(daily(string(yearbirth), "YM")) 
            gen age = year - yearborn
            list
            
                 +----------------------------------+
                 | year   yearbi~h   yearborn   age |
                 |----------------------------------|
              1. | 2000     196010       1960    40 |
              2. | 2005     196112       1961    44 |
              3. | 2013     196103       1961    52 |
              4. | 2004     196209       1962    42 |
                 +----------------------------------+

            Comment

            Working...
            X