DATA Step, Macro, Functions and more

Date in years

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

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; 

 

Please help me regarding this.


Accepted Solutions
Solution
‎10-13-2015 02:08 AM
Respected Advisor
Posts: 4,173

Re: Date in years

[ Edited ]
Posted in reply to KafeelBasha

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.

View solution in original post


All Replies
Super User
Posts: 5,424

Re: Date in years

Posted in reply to KafeelBasha
Why are you starting a new thread?
Data never sleeps
Contributor
Posts: 70

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
Respected Advisor
Posts: 4,173

Re: Date in years

[ Edited ]
Posted in reply to KafeelBasha

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: 70

Re: Date in years

Thanks a lot. It worked

Contributor
Posts: 70

Re: Date in years

Hi Patrick,

 

Can we have the duration in Years Months and days? 

 

Like 1 year 2 months and 3 days.

 

 

Respected Advisor
Posts: 4,173

Re: Date in years

Posted in reply to KafeelBasha

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: 70

Re: Date in years

Is it like, 

 

v1=YearsMonthsAndDays(d1,d2);

 

d1 and d2 are two different dates.

Respected Advisor
Posts: 4,173

Re: Date in years

[ Edited ]
Posted in reply to KafeelBasha

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: 70

Re: Date in years

Helpfull, thanks a lot.

Super User
Posts: 7,761

Re: Date in years

Posted in reply to KafeelBasha

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 344 views
  • 2 likes
  • 4 in conversation