Solved
New Contributor
Posts: 2

# Calculating age between a Date and a Datetime format

It's been over 10 years since I used SAS, so I'm very rusty and was hoping to see if there was a better way of doing this.  I have a database that stores the Date of Birth (DOB) in a datetime informat, and I want to calculate the age using the today() date funciton.  Both dates are stored differently internally (date vs datetime) and thus must be converted to a common denominator before any calculation can be done.  Below is my program, and it works, but is there a better way of doing this?  Thanks!

data demog;
set '/folders/myfolders/Protocols/ATC9701/SAS Data/demog.sas7bdat';
DOBTemp=put(DOB, datetime7.);
DOBFinal = input(DOBTemp, date.);
age = INT(YRDIF(DOBFinal,today(),'ACTUAL'));
run;

Accepted Solutions
Solution
‎03-23-2017 04:08 PM
Super User
Posts: 13,542

## Re: Calculating age between a Date and a Datetime format

Try:

```data demog;
set '/folders/myfolders/Protocols/ATC9701/SAS Data/demog.sas7bdat';
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

DATEPART is the function to extract the date from a datetime variable.

You really should have a library assigned to folders/myfolders/Protocols/ATC9701/SAS Data/

such as

LIbname proj '/folders/myfolders/Protocols/ATC9701/SAS Data';

and then use the library.set notation to refer to data sets.

The change woudl look like:

```data demog;
set proj.demog;
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

which would write the results into your Work library.

Or

```data proj.demog2;
set proj.demog;
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

to write the modified set into the same storage location.

Libraries are a key part of SAS syntax and extremely useful for keeping related data together.

All Replies
Solution
‎03-23-2017 04:08 PM
Super User
Posts: 13,542

## Re: Calculating age between a Date and a Datetime format

Try:

```data demog;
set '/folders/myfolders/Protocols/ATC9701/SAS Data/demog.sas7bdat';
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

DATEPART is the function to extract the date from a datetime variable.

You really should have a library assigned to folders/myfolders/Protocols/ATC9701/SAS Data/

such as

LIbname proj '/folders/myfolders/Protocols/ATC9701/SAS Data';

and then use the library.set notation to refer to data sets.

The change woudl look like:

```data demog;
set proj.demog;
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

which would write the results into your Work library.

Or

```data proj.demog2;
set proj.demog;
age = INT(YRDIF(datepart(DOB),today(),'ACTUAL'));
run;```

to write the modified set into the same storage location.

Libraries are a key part of SAS syntax and extremely useful for keeping related data together.

New Contributor
Posts: 2

## Re: Calculating age between a Date and a Datetime format

I completely forgot about DATEPART! Thank you, and yes, thank you for reminding me about using my libraries. I wrote a quick LIBNAME program to set my library location, I didn't even use them. Thanks for the quick and accurate reply!
☑ This topic is solved.