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;
Please help me regarding this.
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.
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.
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.
Thanks a lot. It worked
Hi Patrick,
Can we have the duration in Years Months and days?
Like 1 year 2 months and 3 days.
Sure you can. You just need to extend the function as posted a bit to calculate months and days instead of days only.
Is it like,
v1=YearsMonthsAndDays(d1,d2);
d1 and d2 are two different dates.
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.
Helpfull, thanks a lot.
Use the floor() and mod() functions to get yeas and remaining days.
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.