Announcement

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

  • 'YRDIF': module to calculate daily date differences

    I thank Kit for making a command yrdif available on SSC. This will calculate the difference between two dates yielding the difference in years. It approximates the SAS function yrdif with basis 'ACTUAL' or 'AGE'. In addition, the option yrunit(ageact) will calculate the fractional part of the as a 365th or 366th. The option, yrunit(age), like the SAS function yrdif with basis 'AGE' calculates the fractional part as a 365th.

    Example

    Code:
    clear
    mat mbdate = (29,29,19,16,28,19,28,29,29,29\ 2, 2, 1, 7, 3,11, 2, 2, 2, 2\1996,1996,2005,2014,1952,1952,2011,2012,2012,1996)
    mat mcdate = (28,29,19,26,19,19,19,19,29,31\ 2, 2, 1,12,10,10,10,10, 2, 8\2000,2000,2020,2019,2012,2012,2012,2012,2012,2013)
    set obs `=colsof(mbdate)'
    gen bdate = mdy(mbdate[2, _n], mbdate[1, _n], mbdate[3, _n])
    gen cdate = mdy(mcdate[2, _n], mcdate[1, _n], mcdate[3, _n])
    format bdate cdate %td
    yrdif bdate cdate , gen(actual) yrunit(actual)
    yrdif bdate cdate , gen(age) yrunit(age)
    yrdif bdate cdate , gen(ageact) yrunit(ageact) snm(agect)
    list
    
    keep bdate
    yrdif bdate , currdate(mdy(2,29,2000)) gen(actual) yrunit(actual)
    yrdif bdate , currdate(mdy(2,29,2000)) gen(age) yrunit(age)
    yrdif bdate , currdate(mdy(2,29,2000)) gen(ageact) yrunit(ageact)
    list

  • #2
    @Allen Buxton Dear Allen, I want to use your command --yrdif--to compute firm age. I get many kinds of age,such as "actual" "age" "ageact",Which kind of age should I use?

    Code:
     . clear
        . mat mbdate = (29,29,19,16,28,19,28,29,29,29\ 2, 2, 1, 7, 3,11, 2, 2, 2, ///
            2\1996,1996,2005,2014,1952,1952,2011,2012,2012,1996)
        . mat mcdate = (28,29,19,26,19,19,19,19,29,31\ 2, 2, 1,12,10,10,10,10, 2,  ///
            8\2000,2000,2020,2019,2012,2012,2012,2012,2012,2013)
        . set obs `=colsof(mbdate)'
        . gen bdate = mdy(mbdate[2, _n], mbdate[1, _n], mbdate[3, _n])
        . gen cdate = mdy(mcdate[2, _n], mcdate[1, _n], mcdate[3, _n])
        . format bdate cdate %td
        . yrdif bdate cdate , gen(actual) yrunit(actual)
        . yrdif bdate cdate , gen(age) yrunit(age)
        . yrdif bdate cdate , gen(ageact) yrunit(ageact) snm(agect)
        . list,noobs
    
      +--------------------------------------------------------------------------------------------+
      |     bdate       cdate      actual         age   agectyrs   agectdys   agectloy      ageact |
      |--------------------------------------------------------------------------------------------|
      | 29feb1996   28feb2000   3.9972678           4          3        365        366   3.9972678 |
      | 29feb1996   29feb2000           4           4          4          0        365           4 |
      | 19jan2005   19jan2020   14.999865          15         15          0        366          15 |
      | 16jul2014   26dec2019   5.4465753   5.4465753          5        163        366   5.4453552 |
      | 28mar1952   19oct2012   60.560109   60.561644         60        205        365   60.561644 |
      |--------------------------------------------------------------------------------------------|
      | 19nov1952   19oct2012   59.915301   59.915068         59        335        366   59.915301 |
      | 28feb2011   19oct2012   1.6389101   1.6383562          1        234        366   1.6393443 |
      | 29feb2012   19oct2012   .63661202   .63835616          0        233        365   .63835616 |
      | 29feb2012   29feb2012           0           0          0          0        365           0 |
      | 29feb1996   31aug2013   17.501812    17.50411         17        184        365    17.50411 |
      +--------------------------------------------------------------------------------------------+
    Code:
    
    
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

    Comment

    Working...
    X