help for yrdif

Calculate daily date differences

yrdif
bdatevar
cdatevar
[if exp] [in range]
,
generate(yearsvar)
[
yrunit(yrunit)
snm(savenameprefix)
]

yrdif
bdatevar
[if exp] [in range]
,
currdate(current_date)
generate(yearsvar)
[
yrunit(yrunit)
snm(savenameprefix)
]

Description

yrdif Calculates people's ages from data on their birth date
and some "current" daily date.

default: yrunit(actual) ... ~SAS yrdif 'ACT/ACT' or equivalently 'ACTUAL'.
option: yrunit(actact) is equivalent.

option: yrunit(age) ... ~SAS yrdif 'AGE' (28Feb=anniversary if
bdatevar=29Feb) per 365 days.

option: yrunit(ageact) ... (28Feb or 29Feb=anniversary if bdatevar=29Feb)
per 365 or 366 days.

There are two syntaxes. In the first, the user supplies two
daily date variables, the first of which is birth date bdatevar and
the second of which is some "current" date cdatevar. As is usual,
such variables may be genuinely variable, taking on different values in
different observations.

In the second syntax, the user supplies a daily date variable,
which is taken to be the birth date bdatevar, and also through the
currdate() option an expression defining the current daily date.

This is motivated by the observation that
SAS ydif( bdatevar , cdatevar , 'ACTUAL')
is used to calculate age. yrdif, by default (yrunit(actual)),
calculates an approximation to
SAS yrdif( bdatevar , cdatevar , 'ACTUAL').
Optionally, yrunit(age) approximates
SAS yrdif( bdatevar , cdatevar , 'AGE').
Lastly, an option yrunit(ageact) calculates the fractional
year as the fraction of days since the last anniversary divided by the
actual days to the next anniversary, 365 or 366. Furthermore, with option
snm(savenameprefix) (1) a new variable is generated containing age in
(completed) years, or the number of anniversaries of the original
date of bdatevar, (2) days since the last anniversary, and (3) days-length of the
current year.

Remarks

yrdif warns if any variable specified has a format
that does not start with "%d" or "%td".

yrdif is not suitable for date-times measured in
milliseconds. Use dofc() or dofC() as appropriate first to
convert to daily dates.

The author's opinion is that the SAS AGE basis is perfect for use
because the integer part closely reflects the common stated age. The
fraction part is on a defined basis of 365ths of a year. It's
consistent with SAS Institutes' AGE basis of YRDIF.

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."

Computing a Person’s Age

"The YRDIF function can compute a person’s age. The first two arguments,
start-date and end-date, are required. If the value of basis is AGE, then YRDIF
computes the age. The age computation takes into account leap years. No other
values for basis are valid when computing a person’s age."

Options

yrunit() By default yrunit(actual), calculates an approximation to
SAS yrdif( , 'ACTUAL'). Optionally, yrunit(age) approximates SAS
yrdif( , 'AGE'). Lastly, an option yrunit(ageact) calculates the fractional
year as the fraction of days since the last birthday divided by the
actual days to the next birthday, 365 or 366

currdate() provides a specification of the "current"
daily date(s) as numeric value(s) through a defining expression.
Commonly, but not necessarily, this will be an expression defining a
constant such as "mdy(10, 20, 2012)". currdate() is required as
an option with the second syntax and not allowed with the first syntax.

snm() specifies one new variable prefix name.
One new variable is generated containing age as number of completed years.
A second new variable is generated containing the number of days since
last anniversary.
A third new variable the number of days in the year that ends with the
next anniversary, which will be 365 or 366 for yrunit(ageact)
and is 365 for yrunit(age).

Examples

. 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

Author

Allen Buxton

abuxton@childrenoncologygroup.org

Acknowledgments

This is a modification of personage by Nicholas J. Cox.

Nicholas J. Cox, Durham University, U.K.

n.j.cox@durham.ac.uk