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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.