## How to calculate age from birthdate using SAS VA

Solved
Occasional Contributor
Posts: 8

# How to calculate age from birthdate using SAS VA

[ Edited ]

Hi,

I'm a total noob in SAS VA. Currently using SAS VA 7.2.

Kindly advise how to calculate age from birthdate using SAS VA.

This should be simple, just substraction between today and birthdate.

I looked at the Operators in New Calculated Item, but nothing worked.

I have searched/googled this topic but what the solutions were using SAS programming or EG, but not applicable in SAS VA.

Thank you.

Accepted Solutions
Solution
‎11-01-2015 02:10 AM
Posts: 1,410

## Re: How to calculate age from birthdate using SAS VA

Welcome to the SAS Visual Analytics community.

As you may have discovered dates in SAS Visual Analytics are not treated the same way as Base SAS. Also the very useful Base SAS date functions such as intck and yrdif are not available.

A date in SAS Visual Analytics is treated as a category data item and not as the traditional SAS date type in the interface. The underlying storage is a SAS date number so you can use the Year function and DatePart function (in case your date is datetime) and you use the TreatAs function to treat the data item as a number if you want to calculate the days. Have a read through the post https://communities.sas.com/t5/SAS-Visual-Analytics/Finding-No-of-Days-between-two-dates-in-SAS-VA/m...

Kind Regards,

Michelle

All Replies
Regular Contributor
Posts: 161

## Re: How to calculate age from birthdate using SAS VA

I'm not sure of SAS VA is different from SAS Base, but ran across this.... might help you...

age = INT(YRDIF(birth-date, ending-date,'ACTUAL'));

https://support.sas.com/publishing/authors/extras/61860_update.pdf

Kannan Deivasigamani
Posts: 5,626

## Re: How to calculate age from birthdate using SAS VA

The syntax is

``age = YRDIF(birthDate, refDate, "AGE");``

where refDate can be Today() if that is what you want.

PG
Solution
‎11-01-2015 02:10 AM
Posts: 1,410

## Re: How to calculate age from birthdate using SAS VA

Welcome to the SAS Visual Analytics community.

As you may have discovered dates in SAS Visual Analytics are not treated the same way as Base SAS. Also the very useful Base SAS date functions such as intck and yrdif are not available.

A date in SAS Visual Analytics is treated as a category data item and not as the traditional SAS date type in the interface. The underlying storage is a SAS date number so you can use the Year function and DatePart function (in case your date is datetime) and you use the TreatAs function to treat the data item as a number if you want to calculate the days. Have a read through the post https://communities.sas.com/t5/SAS-Visual-Analytics/Finding-No-of-Days-between-two-dates-in-SAS-VA/m...

Kind Regards,

Michelle

Occasional Contributor
Posts: 8

## Re: How to calculate age from birthdate using SAS VA

Thank you for all your responses.

Unfortunately SAS VA doesn't support YRDIF.

I followed the link from Michelle with some modification and it worked :-)

Here is what I do:

( TreatAs(_Number_, Now()) - TreatAs(_Number_, 'Birthdate'n) ) / ( 365 * 24 * 60 * 60 )

Regards,

Karina

SAS Employee
Posts: 2

## Re: How to calculate age from birthdate using SAS VA

[ Edited ]

Here is a more precise calculation for age as at today:

IF ( (

IF ( ( ( Year(DatePart(Now())) Mod 4 ) = 0 ) AND ( DayOfYear(DatePart(Now())) > 59 ) )

RETURN ( DayOfYear(DatePart(Now())) - 1 )

ELSE DayOfYear(DatePart(Now())) ) < (

IF ( ( ( Year('Birth Date'n) Mod 4 ) = 0 ) AND ( DayOfYear('Birth Date'n) > 59 ) )

RETURN ( DayOfYear('Birth Date'n) - 1 )

ELSE DayOfYear('Birth Date'n) ) )

RETURN ( ( Year(DatePart(Now())) - Year('Birth Date'n) ) - 1 )

ELSE ( Year(DatePart(Now())) - Year('Birth Date'n) )

This considers the leap years, where 29th February is the 60th day of the leap year. Also replace DatePart(Now()) with a  date variable will give you the difference in year between two dates.

🔒 This topic is solved and locked.

Discussion stats
• 5 replies
• 3179 views
• 4 likes
• 5 in conversation