Contributor
Posts: 59

# In Search of a More Elegant Solution using String Functions

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

Super User
Posts: 10,574

## Re: In Search of a More Elegant Solution using String Functions

``````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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 59

## Re: In Search of a More Elegant Solution using String Functions

Thank you

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

Regards

attachments. -##
Super User
Posts: 13,941

## Re: In Search of a More Elegant Solution using String Functions

And another

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

Though you may want to truncate or round the result.

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