BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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
Meteorite | Level 14

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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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