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;
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.