BookmarkSubscribeRSS Feed
anshul_900
Calcite | Level 5

I need to calculate the difference between two dates in months. I am using the intck function to calculate it with the 4th arguement (method = 'C') but I think the 4th argument just works in case of years. I want to calculate precisely how much is the difference in number of months. For Instance No of Months between 1st July 2018 and 31st dec 2018. The intnx function is returning 5 but which is not correct since its actually over 5 months ( 1 day short of 6 months to be precise). Is there a way to do that? 

5 REPLIES 5
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

add +1.

when you have end_date - start_date you need to account for the starting boundary.

 

PaigeMiller
Diamond | Level 26

Yes, you could calculate the number of months and the number of days in that last fractional month, you would get something like 5 months and 30 days. Is that what you want?

 

I think the 4th argument just works in case of years

 

Incorrect.

--
Paige Miller
Reeza
Super User

@anshul_900 wrote:

I need to calculate the difference between two dates in months. I am using the intck function to calculate it with the 4th arguement (method = 'C') but I think the 4th argument just works in case of years. I want to calculate precisely how much is the difference in number of months. For Instance No of Months between 1st July 2018 and 31st dec 2018. The intnx function is returning 5 but which is not correct since its actually over 5 months ( 1 day short of 6 months to be precise). Is there a way to do that? 


It depends, but you need to be careful of the definition. There is no standard definition of 'month', some have 28 and some have 31 days. That difference can be a big difference when doing comparisons, so this means your data isn't really comparable. For doing survival analysis at a research centre we had settled on using 365.25/12 as the # of days in a month. All that really matters is that you're clear with the definiton you want to use. 1 month and 2 days could be February plus 2 days in January or March, or it could be December which has 31 days and both would be equal. 

 

 

 

 

ballardw
Super User

@anshul_900 wrote:

I need to calculate the difference between two dates in months. I am using the intck function to calculate it with the 4th arguement (method = 'C') but I think the 4th argument just works in case of years. I want to calculate precisely how much is the difference in number of months. For Instance No of Months between 1st July 2018 and 31st dec 2018. The intnx function is returning 5 but which is not correct since its actually over 5 months ( 1 day short of 6 months to be precise). Is there a way to do that? 


The 'C' does not work only with annual. but since your stated example is comparing the first of one month with end of another it may be hard to see what the difference is between 'C' and 'D'. Maybe an examination of this output will shed some light:

data example;
   do sdate= '01Jul2018'd to '31Dec2018'd;
      monthsc = intck('month',sdate,'10Dec2018'd,'C');
      monthsd = intck('month',sdate,'10Dec2018'd,'D');
      output;
   end;
   format sdate date9.;
run;

Pay attention to the differences as SDATE moves from the 9 to 10 and 11 of each month.

 

And what about when you compare 30NOV2018 to 1Dec2018 and get 1 "month" difference? Do you want to round that down because it is only one day?

And you may be learning why some financial processes use a "year" of 360 days with 30 day "months".

Astounding
PROC Star

If you really want fractional months, why go through INTCK?  Why not just convert number of days:

 

n_mon = (end_dt - start_dt) / 30.5;

 

There are more precise formulas, but this one should be close enough.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 2921 views
  • 3 likes
  • 6 in conversation