DATA Step, Macro, Functions and more


Occasional Contributor
Posts: 11


How to calculate the difference between dates when both are character? 

Ex: differnce b/w 01-15-2017 and 01-15-2017 is 'one'(1)  and both are chars. How to find the diff?

Super User
Posts: 13,523

Re: dates

Convert to dates so the SAS date functions work. But first, how do you get a difference of one when both values are 01-15-2017?

Also, do you want the difference in Days, weeks, months, years or something else?

Occasional Contributor
Posts: 11

Re: dates

Thank you for the response. Actually i have two dates vch are the same and i need to output  '1' if the dates r same so i used (end_date-start_date)+1 but its giving a missing value. 

Super User
Posts: 13,523

Re: dates

After looking at the links suggested by @DarthPathos calculate the interval. Then do a test if you need to modify the difference such as:


If difference=0 then difference = 1;


I'm sure I could write a very convoluted one line calculation but it would look ugly or confusing, not be obvious and boil down to the added "if" statement in the end.


If your variables are character then the - should have generated some notes similar to:

NOTE: Invalid numeric data, end_date='01-01-2017' , at line 237 column 11.
NOTE: Invalid numeric data, start_date='01-01-2017' , at line 237 column 20.
end_date=01-01-2017 start_date=01-01-2017 diff=. _ERROR_=1 _N_=1
NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 237:19

Which tells you that you cannot subtract character values (at least those that can not be easily converted to numeric which 01012017 could) and that "missing value" - "missing value" + 1 = "missing value".


Super User
Posts: 6,762

Re: dates

The initial calculation would be:


difference = input(end_date, mmddyy10.) - input(start_date, mmddyy10.);


Whether you want to add either of these is up to you:


difference = difference + 1;




if difference = 0 then difference=1;


There are some "study day" calculations in clinical data that don't allow study day 0.

Super Contributor
Posts: 250

Re: dates

I recommend the paper here- specifically the INPUT and INTCK functions.  As @ballardw indicated, not sure how you get 1 from the dates given, but the INPUT will convert it to the proper format and INTCK will calculate the difference.


Good luck


Has my article or post helped? Please mark as Solution or Like the article!
Ask a Question
Discussion stats
  • 5 replies
  • 4 in conversation