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?
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;
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;
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..
@Naveen45 wrote:
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 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;
@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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.