DATA Step, Macro, Functions and more

How to calculate number of months and weeks between 2 dates?

Reply
Contributor
Posts: 25

How to calculate number of months and weeks between 2 dates?

Hi ,

  

   I need to calculate number of months and weeks between two dates.

Eg. start date ' 01Jan2014' and end date is '14Feb2014'  so the difference is 1.5 months.

Any help is appreciated.

Thanks in advance

Respected Advisor
Posts: 3,156

Re: How to calculate number of months and weeks between 2 dates?

So from February 14th to 28th are all considered as 1.5 month?

Super User
Posts: 5,518

Re: How to calculate number of months and weeks between 2 dates?

Well, it all depends on what is in your data now.  Based on your post, I'm going to assume that you have two character strings, for example:

start_date = '01jan2014';

end_date = '14feb2014';

If your variables actually contain something different, the formula will be different.  Now you can compute:

n_days = input(end_date,date9.) - input(start_date,date9.);

That will give you the difference in days, and you can convert that to weeks or months in any way that pleases you.

Super User
Posts: 19,870

Re: How to calculate number of months and weeks between 2 dates?

That's sort of a difficult question because what is a month isn't clear from month to month. From your example, the duration would actually be 1.4465 months or 1.4 months Smiley Happy

Depending on what you're doing its better to pick either a different interval or some way to standardize it.

I would for example, calculate weeks as days_diff/7.

@ksharp had a good solution for months difference that uses the yrdif function.

data want;

start_date = '01jan2014'd;

end_date = '14feb2014'd;

months=yrdif(start_date, end_date, 'act/act')*12;

weeks=(end_Date-start_date)/7;

run;

Trusted Advisor
Posts: 1,230

Re: How to calculate number of months and weeks between 2 dates?

data want;

  date1='01Jan2014'd;

  date2='14Feb2014'd;

  months=intck('month',date1,date2) + ((intck('days',date1,date2)/31)-1);

run;

Respected Advisor
Posts: 4,934

Re: How to calculate number of months and weeks between 2 dates?

Getting an intuitively correct decimal for the number of months is quite tricky (and likely useless) but here goes:

data _null_;

startDate = '01Jan2014'd;

endDate = '14Feb2014'd;

sb =  intnx("MONTH",startDate,0);

se =  intnx("MONTH",startDate,0,"E");

eb =  intnx("MONTH",endDate,0);

ee =  intnx("MONTH",endDate,0,"E");

m = intck("MONTH",startDate,endDate);

months = round(m - 1 + 2*(se-startDate+endDate-eb+2)/(se-sb+ee-eb+2), 0.1);

weeks = round(intck("DAY", startDate, endDate) / 7, 0.1);

put months= weeks=;

run;

PG

PG
Contributor
Posts: 42

Re: How to calculate number of months and weeks between 2 dates?

check this link.

you will want to use this syntax when you need to be precise.

DATDIF(sdate,edate,basis)

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000530603.htm

Ask a Question
Discussion stats
  • 6 replies
  • 6758 views
  • 2 likes
  • 7 in conversation