## How to convert no of days to years months days format.

Solved
Occasional Contributor
Posts: 11

# How to convert no of days to years months days format.

data Diff;

BDATE='10SEP2008'D;

EDATE='14SEP2010'DD;

ACTDATE=INTCK('DAYS', BDATE, EDATE);

RUN;

OUTPUT IS

 BDATE EDATE ACTDATE 17785 18519 734

I want to convert the above ACTDATE(734days) to 2years0months4days.

What is the code?

Accepted Solutions
Solution
‎08-10-2017 04:24 AM
Super User
Posts: 10,534

## Re: How to convert no of days to years months days format.

Use a succession of intck() and intnx() calls:

``````data diff;
bdate = '10sep2008'd;
edate = '14sep2010'd;
format bdate edate date9.;
actdate = intck('days',bdate,edate);
dify = int(intck('year',bdate,edate));
idate = intnx('year',bdate,dify,'s');
difm = int(intck('month',idate,edate));
idate = intnx('month',idate,difm,'s');
difd = intck('day',idate,edate);
length result \$40;
result = strip(put(dify,3.)) !! 'years' !! strip(put(difm,2.)) !! 'months' !! strip(put(difd,2.)) !! 'days';
drop idate dify difm difd;
run;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Solution
‎08-10-2017 04:24 AM
Super User
Posts: 10,534

## Re: How to convert no of days to years months days format.

Use a succession of intck() and intnx() calls:

``````data diff;
bdate = '10sep2008'd;
edate = '14sep2010'd;
format bdate edate date9.;
actdate = intck('days',bdate,edate);
dify = int(intck('year',bdate,edate));
idate = intnx('year',bdate,dify,'s');
difm = int(intck('month',idate,edate));
idate = intnx('month',idate,difm,'s');
difd = intck('day',idate,edate);
length result \$40;
result = strip(put(dify,3.)) !! 'years' !! strip(put(difm,2.)) !! 'months' !! strip(put(difd,2.)) !! 'days';
drop idate dify difm difd;
run;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 11

## Re: How to convert no of days to years months days format.

@KurtBremser

I just want to know... Is there any fuction which can convert no of days to to yearsmothsdays format directly without writing that entire code..

Super User
Posts: 10,534

## Re: How to convert no of days to years months days format.

Naveen45 wrote:

@KurtBremser

I just want to know... Is there any fuction which can convert no of days to to yearsmothsdays format directly without writing that entire code..

Not that I know of. But thinking again brought me to a simpler solution, but you have to test if it works reliably across dates:

``````data diff;
bdate = '10sep2008'd;
edate = '14sep2010'd;
actdate = intck('days',bdate,edate);
format bdate edate actdate date9.;
dify = year(actdate) - 1960;
difm = month(actdate) - 1;
difd = day(actdate);
run;``````

This uses the fact that the "zero" day for dates in SAS is 1960-01-01.

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

## Re: How to convert no of days to years months days format.

We are close to the output but not exact...
when we change the bdate=24OCT1990 and edate=10AUG2017 we are getting wrong calculation as per the code..
can you fix it.
Super User
Posts: 10,534

## Re: How to convert no of days to years months days format.

Naveen45 wrote:
We are close to the output but not exact...
when we change the bdate=24OCT1990 and edate=10AUG2017 we are getting wrong calculation as per the code..
can you fix it.

I made some small adjustments. Note that the day value will depend on what constitutes "months".

``````data diff1;
bdate = '10sep2008'd;
edate = '14sep2010'd;
bdate = '24oct1990'd;
edate = '10aug2017'd;
format bdate edate date9.;
actdate = intck('days',bdate,edate);
dify = int(intck('year',bdate,edate,'c'));
idate = intnx('year',bdate,dify,'s');
difm = int(intck('month',idate,edate,'c'));
idate = intnx('month',idate,difm,'s');
difd = intck('day',idate,edate);
length result \$40;
result = strip(put(dify,3.)) !! 'years' !! strip(put(difm,2.)) !! 'months' !! strip(put(difd,2.)) !! 'days';
drop idate dify difm difd;
run;

data diff2;
bdate = '24oct1990'd;
edate = '10aug2017'd;
bdate = '10sep2008'd;
edate = '14sep2010'd;
actdate = intck('days',bdate,edate);
format bdate edate actdate date9.;
dify = year(actdate) - 1960;
difm = month(actdate) - 1;
difd = day(actdate) - 1;
result = strip(put(dify,3.)) !! 'years' !! strip(put(difm,2.)) !! 'months' !! strip(put(difd,2.)) !! 'days';
run;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,913

## Re: How to convert no of days to years months days format.

Naveen45 wrote:
We are close to the output but not exact...
when we change the bdate=24OCT1990 and edate=10AUG2017 we are getting wrong calculation as per the code..
can you fix it.

Define "months" in terms of your project. Since months may have 28, 29, 30 or 31 days on the calendar it is quite likely that calendar months any your "month" are not going to align. And actually I'm a bit cautious about "year" as the number of days changes for calendar years.

☑ This topic is solved.