DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

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

BDATEEDATEACTDATE
1778518519734

 

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: 7,832

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

View solution in original post


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

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
Occasional Contributor
Posts: 9

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

Posted in reply to KurtBremser

@KurtBremser

 

Thankyou for your reply...

 

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: 7,832

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


Naveen45 wrote:

@KurtBremser

 

Thankyou for your reply...

 

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
Occasional Contributor
Posts: 9

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

Posted in reply to KurtBremser
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: 7,832

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
Super User
Posts: 11,343

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.

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

Discussion stats
  • 6 replies
  • 184 views
  • 1 like
  • 3 in conversation