BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KafeelBasha
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

10 REPLIES 10
LinusH
Tourmaline | Level 20
Why are you starting a new thread?
Data never sleeps
KafeelBasha
Quartz | Level 8

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.

 

Patrick
Opal | Level 21

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.

KafeelBasha
Quartz | Level 8

Thanks a lot. It worked

KafeelBasha
Quartz | Level 8

Hi Patrick,

 

Can we have the duration in Years Months and days? 

 

Like 1 year 2 months and 3 days.

 

 

Patrick
Opal | Level 21

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

KafeelBasha
Quartz | Level 8

Is it like, 

 

v1=YearsMonthsAndDays(d1,d2);

 

d1 and d2 are two different dates.

Patrick
Opal | Level 21

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. 

KafeelBasha
Quartz | Level 8

Helpfull, thanks a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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