BookmarkSubscribeRSS Feed
shynu
Fluorite | Level 6

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?

5 REPLIES 5
ballardw
Super User

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?

shynu
Fluorite | Level 6

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. 

ballardw
Super User

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

 

Astounding
PROC Star

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;

 

or

 

if difference = 0 then difference=1;

 

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

DarthPathos
Lapis Lazuli | Level 10

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

Chris

Has my article or post helped? Please mark as Solution or Like the article!

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 5 replies
  • 1259 views
  • 2 likes
  • 4 in conversation