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

Hi,

 

Is there a way to get the exact difference in months with decimals between two dates in SAS?

 

data want;

input

date1='01Jan2017'd;

date2='01Jan2018'd;

months1=(date2-date1)/(365.25/12);

run;

 

I have the above method, but not sure it's gives the accurate results...

 

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User
@Data_User you are welcome. If your question has been answered, please mark one of the answers as an accepted solution.
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

View solution in original post

7 REPLIES 7
Data_User
Quartz | Level 8

Expecting the results as follows:

 

Date1Date2Difference in months
28-Mar-0820-Oct-0529.25806452
20-Jun-1111-Mar-0927.29032258
24-Aug-0623-Feb-066.032258065
Quentin
Super User

How are you computing the expected difference in months? 

 

I tried a few different guesses, but none of them matched what you are hoping for:

 

data want;
  input date1 : date9. date2 : date9.;

  days=date2-date1 ;

  months1=(date2-date1)/(365.25/12);
  months2=(date2-date1)/30.5 ;
  months3=(date2-date1)/30 ;

  format date1 date2 date9. ;

cards ;
20Oct2005 28Mar2008
11Mar2009 20Jun2011
23Feb2006 24Aug2006
;
run;

Returns:

    date1        date2    days    months1    months2    months3

20OCT2005    28MAR2008     890    29.2402    29.1803    29.6667
11MAR2009    20JUN2011     831    27.3018    27.2459    27.7000
23FEB2006    24AUG2006     182     5.9795     5.9672     6.0667
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There really isn't any such thing as a fixed unit of month, so you can't really have parts of a month without an approximation when comparing days difference.  However in your case given, the number of months between is:

((year(date2) - year(date1)) * 12) + (month(date2)-month(date1))

So first take number of years difference and each one is 12, then add the difference in months.

 

Just hit post and then thought you could probably do the same far simpler using intnx('month',date1,date2);

Ksharp
Super User
data want;
  input date1 : date9. date2 : date9.;

  days=yrdif(date1,date2,'age')*12;
  format date1 date2 date9. ;

cards ;
20Oct2005 28Mar2008
11Mar2009 20Jun2011
23Feb2006 24Aug2006
;
run;
Quentin
Super User
@Data_User you are welcome. If your question has been answered, please mark one of the answers as an accepted solution.
The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 3590 views
  • 4 likes
  • 5 in conversation