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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: 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
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: 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.
BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

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!

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.

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
  • 3047 views
  • 4 likes
  • 5 in conversation