BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rudy
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

2 REPLIES 2
ballardw
Super User

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.

Rudy
Calcite | Level 5
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!

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
  • 2 replies
  • 2782 views
  • 1 like
  • 2 in conversation