DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Contributor ykk
Contributor
Posts: 24
Accepted Solution

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

View solution in original post


All Replies
Super User
Posts: 10,483

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 ykk
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: 10,483

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

Trusted Advisor
Posts: 1,115

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
Super User
Posts: 6,499

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 6344 views
  • 0 likes
  • 5 in conversation