Solved
Contributor
Posts: 71

# Date in years

In the below code I would like to see 'b' as years and days format.

Let b=9.19, then I would like to see this as 9 years 191 days format (years and days).

data dt;
a=today()-'01AUG2006'd;
b=a/365;
run;

Accepted Solutions
Solution
‎10-13-2015 02:08 AM
Posts: 4,743

## Re: Date in years

[ Edited ]

Leap years complicate things and it depends on the start and end year to how many days and years a difference in days translates.

That's why I believe you can't get this 100% right with a format applied on the number of days as there you've lost the information already which one had been the start year and which one the end year (to take leap years into account).

Below code option should return what you're asking for taking leap years into account.

``````
proc fcmp outlib=work.funcs.years;
function YearsAndDays(startdt, enddt) \$;
years=intck('year',startdt, enddt,'c');
days=enddt-intnx('year',startdt,years,'s');
length outval \$20;
outval=cat(years,' years ',days,' days');
return(outval);
endsub;
run;

options cmplib=work.funcs;

data sample;
var=YearsAndDays('01AUG2006'd, today());
output;
var=YearsAndDays('29Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2011'd, '28Feb2012'd);
output;
var=YearsAndDays('28Feb2011'd, '29Feb2012'd);
output;
run;
``````

Code above fixed as per @PGStats feedback.

All Replies
Super User
Posts: 5,888

## Re: Date in years

Why are you starting a new thread?
Data never sleeps
Contributor
Posts: 71

## Re: Date in years

I can understand, but is there a way we can get result as I mentioned year and days.

Is there any format to be defined.

Solution
‎10-13-2015 02:08 AM
Posts: 4,743

## Re: Date in years

[ Edited ]

Leap years complicate things and it depends on the start and end year to how many days and years a difference in days translates.

That's why I believe you can't get this 100% right with a format applied on the number of days as there you've lost the information already which one had been the start year and which one the end year (to take leap years into account).

Below code option should return what you're asking for taking leap years into account.

``````
proc fcmp outlib=work.funcs.years;
function YearsAndDays(startdt, enddt) \$;
years=intck('year',startdt, enddt,'c');
days=enddt-intnx('year',startdt,years,'s');
length outval \$20;
outval=cat(years,' years ',days,' days');
return(outval);
endsub;
run;

options cmplib=work.funcs;

data sample;
var=YearsAndDays('01AUG2006'd, today());
output;
var=YearsAndDays('29Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2011'd, '28Feb2012'd);
output;
var=YearsAndDays('28Feb2011'd, '29Feb2012'd);
output;
run;
``````

Code above fixed as per @PGStats feedback.

Contributor
Posts: 71

## Re: Date in years

Thanks a lot. It worked

Contributor
Posts: 71

## Re: Date in years

Hi Patrick,

Can we have the duration in Years Months and days?

Like 1 year 2 months and 3 days.

Posts: 4,743

## Re: Date in years

Sure you can. You just need to extend the function as posted a bit to calculate months and days instead of days only.

Contributor
Posts: 71

## Re: Date in years

Is it like,

v1=YearsMonthsAndDays(d1,d2);

d1 and d2 are two different dates.

Posts: 4,743

## Re: Date in years

[ Edited ]

No, it's extending the function as below

``````proc fcmp outlib=work.funcs.years;
function YearsAndDays(startdt, enddt) \$;
years=intck('year',startdt, enddt,'c');

shift_dt=intnx('year',startdt,years,'s');
months=intck('month',shift_dt, enddt,'c');

shift_dt=intnx('month',shift_dt,months,'s');
days=enddt-shift_dt;

length outval \$30;
outval=cat(years,' years ',months,' months ',days,' days');
return(outval);
endsub;
run;

options cmplib=work.funcs;

data sample;
var=YearsAndDays('01AUG2006'd, today());
output;
var=YearsAndDays('29Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2012'd, '28Feb2013'd);
output;
var=YearsAndDays('28Feb2011'd, '28Feb2012'd);
output;
var=YearsAndDays('28Feb2011'd, '29Feb2012'd);
output;
run;
``````

Question is: What is a year? Is 01jan2012 to 01jan2013 a year or a year and a day? Depends how you look at it I guess and if you interprete your end-date as exclusive or inclusive.

Contributor
Posts: 71

Super User
Posts: 10,280

## Re: Date in years

Use the floor() and mod() functions to get yeas and remaining days.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
🔒 This topic is solved and locked.