Solved
Contributor
Posts: 58

# better ways to display age

I am looking for a solution to elegantly display age.

Say I have two datetime varoiables: DOB and a variable called School Entry, at which age were the children schooled ?

Time_in_Seconds = (School Entry - DOB) - what is the best way to convert this into age including years, months and days?

Often it is just displayed in years, but I would want a more exact approach, therefore something like : Tane is 12 years, 5 months and 4 days old.

I could create someting over multiple colums, whereby

age_min  =ROUND ((age_sec)/60);
age_h    =ROUND  ((age_sec)/3600);
age_d     =ROUND ((age_sec)/86400);
age_y   =ROUND ((age_sec)/31536000);

but that is not very elegant. Would anybody know a better way to deal with this?

many thanks

Accepted Solutions
Solution
‎06-22-2017 08:14 PM
Super User
Posts: 3,918

## Re: better ways to display age

[ Edited ]

Try the INTCK function. This will adjust properly for issues like leap years, differing number of days in months for example.

``years = intck('dtyear', DOB, SchoolEntry, 'C');months = intck('dtmonth', DOB, SchoolEntry, 'C');``

Check out the documentation here:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1md4mx2crz...

All Replies
Solution
‎06-22-2017 08:14 PM
Super User
Posts: 3,918

## Re: better ways to display age

[ Edited ]

Try the INTCK function. This will adjust properly for issues like leap years, differing number of days in months for example.

``years = intck('dtyear', DOB, SchoolEntry, 'C');months = intck('dtmonth', DOB, SchoolEntry, 'C');``

Check out the documentation here:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1md4mx2crz...

Posts: 1,837

## Re: better ways to display age

Asuming DIF is the difference in seconds then you may try

age = put(datepart(DIF),yymmdd10.);

SAS Super FREQ
Posts: 825

## Re: better ways to display age

Find below a sample program that calculates the age in years months and days.

The sample is based on a SAS date value

``````data ageCalc;
infile cards dlm=",";
input
min_date : date9.
max_date : date9.
;
years    = intck('year',min_date,max_date,'c');
months   = intck('month',intnx('year',min_date,years,'same'),max_date,'c');
days     = intck('day',intnx('month',intnx('year',min_date,years,'same'),months,'same'),max_date,'c');
format min_date max_date ddmmyyp10.;
cards;
10NOV1963,26sep2013
15mar1961,11oct2001
31jan2000,01mar2000
29feb2000,28feb2001
;

proc print data=ageCalc;
run;``````
☑ This topic is solved.