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

 

 

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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;
Naveen45
Fluorite | Level 6

@Kurt_Bremser

 

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..

Kurt_Bremser
Super User

@Naveen45 wrote:

@Kurt_Bremser

 

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.

Naveen45
Fluorite | Level 6
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.
Kurt_Bremser
Super User

@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;
ballardw
Super User

@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.

 

 

Ajayvit
Fluorite | Level 6

data demo2;
set demo;
days=intck('Day',bdate,svdate); /*how to conver this days into year*/
months=intck('Months',bdate,svdate);
year=intck('Years',bdate,svdate);
inyear=int(days/365);  /*this will convert days into year and  int which I have mention will eliminate decimal value and give year*/
run;

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
  • 7 replies
  • 9912 views
  • 1 like
  • 4 in conversation