Announcement

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

  • Addition of date / age

    Hello ,

    I'm working on computing age of firms from incorporation date. Some of the dates in my data are listed in dd/mm/yyyy format while others in yyyy format. I used the following commands to generate two variables date and date1 and age1 and age2 :

    Code:
    gen int date = daily( INCORPORATION_DATE , "DMY", 2019
    gen int date1 = daily( INCORPORATION_DATE , "Y", 2019)
    gen age1= (td(1jan2020) - date1 )/365.25
    gen age2 = (td(1jan2020) - date)/365.25
    Please see below the sample data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 INCORPORATION_DATE int(date date1) float(age1 age2)
    "29/12/1992"  12051      .         .  27.00616
    "27/09/1969"   3557      .         .  50.26146
    "30/06/1992"  11869      .         .  27.50445
    "11/06/2007"  17328      .         .  12.55852
    "07/05/1962"    857      .         .  57.65366
    "03/09/1985"   9377      .         .  34.32717
    "09/10/2008"  17814      .         . 11.227926
    "24/04/1989"  10706      .         .  30.68857
    "26/09/2000"  14879      .         .  19.26352
    "23/04/1993"  12166      .         . 26.691307
    "02/04/2004"  16163      .         . 15.748117
    "19/11/2007"  17489      .         . 12.117727
    "21/11/1985"   9456      .         .  34.11088
    "19/09/2000"  14872      .         .  19.28268
    "21/06/2006"  16973      .         . 13.530458
    "20/12/2006"  17155      .         .  13.03217
    "29/06/2007"  17346      .         .  12.50924
    "10/12/2010"  18606      .         .  9.059548
    "18/07/2001"  15174      .         . 18.455853
    "17/09/2008"  17792      .         . 11.288158
    "22/02/2010"  18315      .         .  9.856263
    "01/11/2005"  16741      .         .  14.16564
    "26/09/2002"  15609      .         . 17.264887
    "22/07/2002"  15543      .         . 17.445585
    "27/07/1993"  12261      .         .  26.43121
    "08/09/1925" -12533      .         .  94.31348
    "1902"            . -21184 117.99863         .
    "04/03/2003"  15768      .         . 16.829569
    "05/10/2002"  15618      .         . 17.240246
    "21/05/2007"  17307      .         . 12.616016
    end

    I want to generate a single column for age by merging age1 and age2 variables. Could someone please help with the code or guide me as to a better way to do this.

    Thank you.
    Last edited by Shabeen Basha; 25 Jan 2021, 01:47. Reason: Correct the format for posting code and sample data

  • #2
    Shabeen:
    I'm not sure I get you right.
    Anyway, you may want to consider:
    Code:
    . format date1 %td
    
    . format date %td
    
    . g age_cum=age2
    
    
    . replace age_cum=age1 if age_cum==. & age1!=.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thanks a lot Carlo, you got me right and the code in #2 gave me what I required.

      Best regards,
      Shabeen

      Comment


      • #4
        @Shabeen Basha You can use the SSC command --yrdif--to compute firm age,--yrdif--is a modification of --personage(SSC)-- by @Nick Cox . Also,use SSC command --numdate--to transfer date.
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str10 INCORPORATION_DATE
        "29/12/1992"
        "27/09/1969"
        "30/06/1992"
        "11/06/2007"
        "07/05/1962"
        "03/09/1985"
        "09/10/2008"
        "24/04/1989"
        "26/09/2000"
        "23/04/1993"
        "02/04/2004"
        "19/11/2007"
        "21/11/1985"
        "19/09/2000"
        "21/06/2006"
        "20/12/2006"
        "29/06/2007"
        "10/12/2010"
        "18/07/2001"
        "17/09/2008"
        "22/02/2010"
        "01/11/2005"
        "26/09/2002"
        "22/07/2002"
        "27/07/1993"
        "08/09/1925"
        "1902"      
        "04/03/2003"
        "05/10/2002"
        "21/05/2007"
        end
        Code:
        numdate d bdate=INCORPORATION_DATE,pattern(DMY)
        numdate d date1=INCORPORATION_DATE,pattern(Y)
        replace bdate=date1 if mi(bdate)
        drop date1
        gen date="1jan2020"
        numdate d cdate=date,pattern(DMY)
        yrdif bdate cdate, gen(ageact) yrunit(ageact) snm(agect)
        
        list INCORPORATION_DATE bdate cdate ageact,sep(10) noobs
        
         +------------------------------------------------+
          | INCORPOR~E       bdate       cdate      ageact |
          |------------------------------------------------|
          | 29/12/1992   29dec1992   01jan2020   27.008197 |
          | 27/09/1969   27sep1969   01jan2020   50.262295 |
          | 30/06/1992   30jun1992   01jan2020   27.505464 |
          | 11/06/2007   11jun2007   01jan2020   12.557377 |
          | 07/05/1962   07may1962   01jan2020   57.653005 |
          | 03/09/1985   03sep1985   01jan2020   34.327869 |
          | 09/10/2008   09oct2008   01jan2020   11.229508 |
          | 24/04/1989   24apr1989   01jan2020   30.688525 |
          | 26/09/2000   26sep2000   01jan2020   19.265027 |
          | 23/04/1993   23apr1993   01jan2020   26.691257 |
          |------------------------------------------------|
          | 02/04/2004   02apr2004   01jan2020   15.748634 |
          | 19/11/2007   19nov2007   01jan2020   12.117486 |
          | 21/11/1985   21nov1985   01jan2020   34.112022 |
          | 19/09/2000   19sep2000   01jan2020   19.284153 |
          | 21/06/2006   21jun2006   01jan2020   13.530055 |
          | 20/12/2006   20dec2006   01jan2020   13.032787 |
          | 29/06/2007   29jun2007   01jan2020   12.508197 |
          | 10/12/2010   10dec2010   01jan2020   9.0601093 |
          | 18/07/2001   18jul2001   01jan2020   18.456284 |
          | 17/09/2008   17sep2008   01jan2020   11.289617 |
          |------------------------------------------------|
          | 22/02/2010   22feb2010   01jan2020   9.8575342 |
          | 01/11/2005   01nov2005   01jan2020   14.166667 |
          | 26/09/2002   26sep2002   01jan2020   17.265027 |
          | 22/07/2002   22jul2002   01jan2020   17.445355 |
          | 27/07/1993   27jul1993   01jan2020   26.431694 |
          | 08/09/1925   08sep1925   01jan2020   94.314208 |
          |       1902   01jan1902   01jan2020         118 |
          | 04/03/2003   04mar2003   01jan2020   16.827869 |
          | 05/10/2002   05oct2002   01jan2020   17.240437 |
          | 21/05/2007   21may2007   01jan2020   12.614754 |
          +------------------------------------------------+
        Last edited by Raymond Zhang; 25 Jan 2021, 02:44.
        Best regards.

        Raymond Zhang
        Stata 17.0,MP

        Comment


        • #5
          Dear Raymond,

          Thank you for the yrdif and numdate codes . The code in #4 generated age for some observations the previous approach (OP and code in #3) didn't , for some years in 1800s (not all of them though).

          Best regards,
          Shabeen

          Comment


          • #6
            @Shabeen Basha Sorry for my poor English.So,I want know which codes you use at last.My codes or your codes?I find that the firm age calculated by the two methods is slightly different.
            Best regards.

            Raymond Zhang
            Stata 17.0,MP

            Comment


            • #7
              If you see the help file of -yrdif-, you will see the words below:
              Code:
               SAS YRDIF is a financial calculation.
                  Refer to: SAS 9.4 Functions and CALL Routines: Reference, Fifth Edition
              
                  Calculations That Use ACT/ACT Basis
                  "In YRDIF calculations that use the ACT/ACT basis, both a 365–day year and
                  366–day year are taken into account. For example, if n365 equals the number of
                  days between the start and end dates in a 365–day year, and n366 equals the
                  number of days between the start and end dates in a 366–day year, the YRDIF
                  calculation is computed as YRDIF=n365/365.0 + n366/366.0.  This calculation
                  corresponds to the commonly understood ACT/ACT day count basis that is
                  documented in the financial literature."
              Through these words,I think that Use ACT/ACT Basis is a better way to compute firm age in the financial literature.

              Kind Regards.
              Raymond
              Best regards.

              Raymond Zhang
              Stata 17.0,MP

              Comment


              • #8
                Raymond Zhang , I finally used the -yrdif- and -numdate- codes (suggested by you in #4) as they computed the age for some years that were missed by the code in the original post.

                Thank you once again.

                Best regards,
                Shabeen

                Comment


                • #9
                  @Shabeen Basha In fact,if you corrected your codes(OP and code in #3),you can also get all the firm age without mising values.
                  Code:
                   gen  date = daily( INCORPORATION_DATE , "DMY", 2019)
                   gen  date1 = daily( INCORPORATION_DATE , "Y", 2019)
                   gen age1= (td(1jan2020) - date1 )/365.25
                   gen age2 = (td(1jan2020) - date)/365.25
                   format date1 %td
                   format date %td
                   g age_cum=age2
                   replace age_cum=age1 if age_cum==. & age1!=.
                  Best regards.

                  Raymond Zhang
                  Stata 17.0,MP

                  Comment


                  • #10
                    You should not use "int" in the first two lines of your codes.
                    Best regards.

                    Raymond Zhang
                    Stata 17.0,MP

                    Comment


                    • #11
                      FYI, in recent updates of Stata 16.1, over 20 functions for date and time were added, some of which may be relevant here. Examples: function age() returns the age in integer years; age_frac() returns the age in years, including the fractional part; datediff() returns the integer difference between two dates in a given unit (year, month, or day); datediff_frac() returns the same difference, including the fractional part.

                      All these functions factor in leap days in the calculation. An option is also provided to specify whether February 28th or March 1st is the nonleap-year birthday for February 29th birthdates.

                      Other related functions include birthday(), previousbirthday(), and nextbirthday(). Type update all for the latest update, and help datetime functions for more details.

                      -- Kreshna

                      Comment


                      • #12
                        Originally posted by Raymond Zhang View Post
                        @Shabeen Basha In fact,if you corrected your codes(OP and code in #3),you can also get all the firm age without mising values.
                        Code:
                        gen date = daily( INCORPORATION_DATE , "DMY", 2019)
                        gen date1 = daily( INCORPORATION_DATE , "Y", 2019)
                        gen age1= (td(1jan2020) - date1 )/365.25
                        gen age2 = (td(1jan2020) - date)/365.25
                        format date1 %td
                        format date %td
                        g age_cum=age2
                        replace age_cum=age1 if age_cum==. & age1!=.
                        Raymond Zhang Indeed, the updated codes gave me the same result (in terms of observations) with a few decimal differences in values.

                        Thank you once again.

                        Best regards,
                        Shabeen

                        Comment


                        • #13
                          Kreshna Gopal (StataCorp) , thank you for the information in #11. I'll try the new functions after updating my current version of Stata 15.1 SE.

                          Best regards,
                          Shabeen

                          Comment

                          Working...
                          X