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
So from February 14th to 28th are all considered as 1.5 month?
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.
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
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;
data want;
date1='01Jan2014'd;
date2='14Feb2014'd;
months=intck('month',date1,date2) + ((intck('days',date1,date2)/31)-1);
run;
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
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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.