Editor's note: This is a popular topic. We have summarized the best answers into this single reply to help you find the answer you need. But first, here's a video that addresses common questions around SAS date functions. Since it's 30+ minutes long, below are timestamps to guide you to the tip relevant to you:
00:16 – SAS Dates Explored
06:07 – Example: How to convert a character form of a date to a SAS Date value using the SAS INPUT function
16:46 – Example: How to determine time passed between two dates using the YRDIF function
19:08 – SAS Times Explored
22:15 – Example: How to convert a character form of a time to a SAS Time value using the SAS INPUT function
26:36 – SAS DateTime Explored
32:04 – Example: How to convert a character form of a DateTime to a SAS DateTime value using the SAS INPUT function
Related content
Paper: The Essentials of SAS Dates and Times
Book: SAS Functions by Example, Second Edition
Community article: INTNX and INTCK Function Examples
Blog post: Do you mind if we dance with your DATEs (or DATETIMEs)?
Below are aggregated "best answers" to this community topic
From @cov_derek:
"SAS-formatted dates" are stored as an integer representing numer of days relative to January 1, 1960. This makes it easy to do simple calculations involving dates. In general, the difference between two SAS dates in days can most easily be calculated as duration=end_date - start_date.
Sample SQL code to calculate:
proc sql;
create table Stay as
select
t1.id,
min(date_1) as Admit_Date,
max(date_2) as Discharge_Date,
max(date_2) - min(date_1) as Length_of_stay
from
have t1
group by t1.id;
quit;
You'd have to decide how to handle the case where patients were discharged the same day they were admitted.
The INTCK function in SAS can calculate the difference between any two dates or datetime values, and return whatever interval you're looking for (days, minutes, hours, weeks, months). @dgammon shares this example of using INTCK with datetime variables to calculate a day interval.
data test;
set test;
by subj;
days=intck('dtday', date1, date2);
put days=;
run;
For hospital stays, you might have special rules about calculating day intervals. For example, a patient who is admitted and charged on the same day might count as a 1-day stay, and a patient who is discharged on the second day might still count as a 1-day stay -- so you might need to modify the formula to add 1 in the case of a same-day discharge. If your data are such that any record with a hospital admission represents at least a 1-day stay (for reporting purposes), then your formula might be:
/* return the higher of two values: calculated interval or 1 */
dur = max(intck('day',date_1,date_2), 1);
... View more