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"))
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.
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;
@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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.