BookmarkSubscribeRSS Feed
mimi
Calcite | Level 5

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

6 REPLIES 6
Haikuo
Onyx | Level 15

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

Astounding
PROC Star

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.

Reeza
Super User

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;

stat_sas
Ammonite | Level 13

data want;

  date1='01Jan2014'd;

  date2='14Feb2014'd;

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

run;

PGStats
Opal | Level 21

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
econ
Quartz | Level 8

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

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!

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
  • 6 replies
  • 17680 views
  • 2 likes
  • 7 in conversation