SAS Code to Calculate difference between two dates in number of Days without use of sas functions like DATDIF and INTCK
I was able to get the correct answer i was trying to find the diff in days between a value which is in General date format and current date using date() function.
I had used datepart() on the date value I had got and now I am getting correct answer.Thanks for the help
date() - datepart(<datevariable>).
Without those functions the only thing I would count on being accurate is the difference in days
Difference = LaterDate - PreviousDate;
If you want weeks, months or anything else you'll have to write it yourself and is a real waste of time.
What can't you use the SAS date functions?
I had tried that but it is not accurate giving more days than expected
Result:20059
Dates Compared 13-JUN-2016 and 02-DEC-2014
7 data;
8 start = '02dec2014'd;
9 end = '13jun2016'd;
10 diff = end - start;
11 put 'diff=' diff;
12 run;
diff=559
Were the values actually SAS date value or something like 20160613 and 20141202 (which are NOT SAS date values)?
Your variables do not contain dates. To get a difference of 20059 from an actual 2DEC2014 date you would need to have 2Nov2069.
Please try printing some of those values with a format of BEST16. and showing them here.
I was able to get the correct answer i was trying to find the diff in days between a value which is in General date format and current date using date() function.
I had used datepart() on the date value I had got and now I am getting correct answer.Thanks for the help
date() - datepart(<datevariable>).
Hey I have the same problem. How did u solve it? I have a big dataset with over 300 observations and I have the startingdate and the endingdate dd/mm/yyyy and I want to calculate the duration for each observation. Can you write me the code for that? I didnt quite understand it 😞
Thank you so much! Cheers, Mary
@ykk: I suggest you double-check the code which gave you the result 20059 in view of the fact that 20059='02DEC2014'd.
Getting a difference in the range of 20,000 when talking about a two year difference in dates make it look like you are taking the difference between two integers that just look to you like they are dates since they happen to be in the range between 20,140,000 and 20,170,000.
Try converting the values to dates first and then taking the difference.
diff = input(put(date1,8.),yymmdd8.) - input(put(date2,8.),yymmdd8.) ;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.