Solved
Contributor
Posts: 24

# SAS Code to Calculate difference between two dates in number of Days

SAS Code to Calculate difference between two dates in number of Days without use of sas functions like DATDIF and INTCK

Accepted Solutions
Solution
‎06-13-2016 03:51 PM
Contributor
Posts: 24

## Re: SAS Code to Calculate difference between two dates in number of Days

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>).

All Replies
Super User
Posts: 13,508

## Re: SAS Code to Calculate difference between two dates in number of Days

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?

Contributor
Posts: 24

## Re: SAS Code to Calculate difference between two dates in number of Days

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

Super Contributor
Posts: 394

## Re: SAS Code to Calculate difference between two dates in number of Days

```7 data;8 start = '02dec2014'd;9 end = '13jun2016'd;10 diff = end - start;11 put 'diff=' diff;12 run;diff=559
```
Super User
Posts: 13,508

## Re: SAS Code to Calculate difference between two dates in number of Days

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.

Solution
‎06-13-2016 03:51 PM
Contributor
Posts: 24

## Re: SAS Code to Calculate difference between two dates in number of Days

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>).

Contributor
Posts: 43

## Re: SAS Code to Calculate difference between two dates in number of Days

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

Posts: 1,245

## Re: SAS Code to Calculate difference between two dates in number of Days

@ykk: I suggest you double-check the code which gave you the result 20059 in view of the fact that 20059='02DEC2014'd.

Super User
Posts: 8,075

## Re: SAS Code to Calculate difference between two dates in number of Days

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.) ;

☑ This topic is solved.