BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ykk
Obsidian | Level 7 ykk
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ykk
Obsidian | Level 7 ykk
Obsidian | Level 7

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

8 REPLIES 8
ballardw
Super User

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?

ykk
Obsidian | Level 7 ykk
Obsidian | Level 7

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

Tim_SAS
Barite | Level 11
7 data;
8 start = '02dec2014'd;
9 end = '13jun2016'd;
10 diff = end - start;
11 put 'diff=' diff;
12 run;

diff=559
ballardw
Super User

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.

 

ykk
Obsidian | Level 7 ykk
Obsidian | Level 7

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

marysmith
Calcite | Level 5

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

FreelanceReinh
Jade | Level 19

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

Tom
Super User Tom
Super User

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

 

 

 

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
  • 8 replies
  • 29047 views
  • 0 likes
  • 6 in conversation