BookmarkSubscribeRSS Feed
JonDickens1607
Obsidian | Level 7

Given the following SAS Code which is applied to a SAS Date Set containing several explanatory variables and specifically a CHAR Variable: MAIN_DOB which contains the date of birth of the main applicant in the following format: 1978-06-28

 

We need to calculate the age of the applicant in years at the time of contract initiation.

 

Contract_Start_Date_DT is a SAS Date in the format 25/06/2014

 

The following SAS Code works but surely there is a more elegant / efficient solution  to this simple problem

 

DATA
LIBXYZ.DEVELOPMENT_SAMPLE_03 ;

SET LIBXYZ.DEVELOPMENT_SAMPLE_02 ;

SearchDate_DT = DATEPART(SearchDate) ;
QuoteDate_DT = DATEPART(QuoteDate) ;
Contract_StartDate_DT = DATEPART(Contract_StartDate) ;
Vehicle_RegistrationDate_DT = DATEPART(Vehicle_RegistrationDate) ;
Termination_Date_DT = DATEPART(Termination_Date) ;
Termination_CarDisposalDate_DT = DATEPART(Termination_CarDisposalDate) ;

 

DateOfBirth_Char = INPUT( COMPRESS( MAIN_DOB,"-" ) , $8. ) ;

DateOfBirth_Year = INPUT( SUBSTR(DateOfBirth_Char , 1 , 4 ) , 4. ) ;

DateOfBirth_Month = INPUT( SUBSTR(DateOfBirth_Char , 5 , 2 ) , 2. ) ;

 

IF DateOfBirth_Month < 10
THEN DateOfBirth_Month1 = CAT('0' , DateOfBirth_Month ) ;
ELSE DateOfBirth_Month1 = DateOfBirth_Month ;

DateOfBirth_Day = INPUT( SUBSTR(DateOfBirth_Char , 7 , 2 ) , 2. ) ;

 

IF DateOfBirth_Day < 10
THEN DateOfBirth_Day1 = CAT('0' , DateOfBirth_Day ) ;
ELSE DateOfBirth_Day1 = DateOfBirth_Day ;

DateOfBirth = INPUT(CATX( '/' , DateOfBirth_Day1 , DateOfBirth_Month1 , DateOfBirth_Year ) , ddmmyy10. ) ;

 

FORMAT SearchDate_DT QuoteDate_DT Contract_StartDate_DT Vehicle_RegistrationDate_DT Termination_Date_DT Termination_CarDisposalDate_DT ddmmyy10. ;


FORMAT DateOfBirth ddmmyy10. ;

CONTRACT_AGE = ( YEAR(Contract_StartDate_DT) - DateOfBirth_Year ) ;

FORMAT CONTRACT_AGE 4. ;

 

DROP SearchDate QuoteDate Contract_StartDate Vehicle_RegistrationDate Termination_Date Termination_CarDisposalDate
DateOfBirth_Char DateOfBirth_Year DateOfBirth_Month DateOfBirth_Day DateOfBirth_Day1 DateOfBirth_Month1 ;


RUN;

 

Thanks for kind assistance

3 REPLIES 3
Kurt_Bremser
Super User
date_of_birth = input(main_dob,yymmdd10.);
format date_of_birth ddmmyys10.;

contract_age = intck('year',date_of_birth,contract_start_date_dt);
/* or */
contract_age = year(contract_start_date_dt) - year(date_of_birth);
/* or */
contract_age = (contract_start_date_dt - date_of_birth) / 365.25;

Once you have a date as SAS date (days from 01/01/1960), such calculations become easy.

JonDickens1607
Obsidian | Level 7
Thank you

I will implement this and let you know if it works in my application.

Regards

##- Please type your reply above this line. Simple formatting, no
attachments. -##
ballardw
Super User

And another

contract_age = yrdif(date_of_birth,contract_start_date_dt,'ACT/ACT');

 

Though you may want to truncate or round the result.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 954 views
  • 3 likes
  • 3 in conversation