turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- dates

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 01:14 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 01:24 PM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 01:28 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 01:40 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 03:16 PM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-20-2017 01:28 PM

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!