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
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
Comment