BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

What is the most accurate way to calculate exact age in years that including decimal part (For example: 40.8).

I saw the following ways and not sure which of them is better and maybe there is a better way?


age = (today() - birth) / 365.25

age=floor(YRDIF(birth_Date, today(),"AGE"))
4 REPLIES 4
PaigeMiller
Diamond | Level 26

As always, context is everything, and you have given us no context.

 

Why do you need decimal years, what will you do with this information? We can't tell you which is better unless we know this. What is wrong with calculated years/days between two dates (like someone is 27 years, 228 days old)? Why are you comparing a method that returns a decimal number of years with a method that returns an integer number of year? How can these be compared?

 

Also, different cultures calculate ages in different ways, what calendar/what culture?

 

Please provide example SAS code with semi-colons at the end of a SAS statement. Thanks.

--
Paige Miller
Ronein
Onyx | Level 15

I want to calculate exact age (with decimal ) in years and then calculate rounded age .(with floor function)

For example:

If exact age is 18.02 then rounded age is 18

If exact age is 17.9 then rounded age is 17


data have;
format birth_MIN today ddmmyy10.;
input birth_MIN :date9. today :date9.;
cards;
28Dec1978 31DEC2023
15FEB1978 31DEC2023
22Dec2005 31DEC2023
;
Run;
data want;
set have;
age1=yrdif(birth_MIN,today,'AGE');
age2=sum(today,-birth_MIN)/365.25;
age3=(intck('month',birth_MIN,today)-(day(today)<day(birth_MIN)))/12;
rounded_age1=Floor(age1);
rounded_age2=Floor(age2);
rounded_age3=Floor(age3);
Run;

 

PaigeMiller
Diamond | Level 26

@Ronein wrote:

I want to calculate exact age (with decimal ) in years and then calculate rounded age .(with floor function)

For example:

If exact age is 18.02 then rounded age is 18

If exact age is 17.9 then rounded age is 17


data have;
format birth_MIN today ddmmyy10.;
input birth_MIN :date9. today :date9.;
cards;
28Dec1978 31DEC2023
15FEB1978 31DEC2023
22Dec2005 31DEC2023
;
Run;
data want;
set have;
age1=yrdif(birth_MIN,today,'AGE');
age2=sum(today,-birth_MIN)/365.25;
age3=(intck('month',birth_MIN,today)-(day(today)<day(birth_MIN)))/12;
rounded_age1=Floor(age1);
rounded_age2=Floor(age2);
rounded_age3=Floor(age3);
Run;

 


Now I'm lost. Your original post did not use FLOOR on the first method of computing years with decimals.

 

Also, I did ask you to explain what you are going to do with this calculated age, you have not explained anything. We can't know which is better unless you tell us what you are going to do with these calculated ages.

--
Paige Miller
Quentin
Super User

Art Carpenter wrote a brief paper on some age calculation options:

https://www.lexjansen.com/wuss/2010/coders/2943_4_COD-Carpenter.pdf

 

There is no "best" way, it really varies with your goal/purpose.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2054 views
  • 0 likes
  • 3 in conversation