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
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.
And another
contract_age = yrdif(date_of_birth,contract_start_date_dt,'ACT/ACT');
Though you may want to truncate or round the result.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.