Hey,
my boss wants to know how long does a therapy last. This means the difference between the start date and end date. That can be days, weeks or months. The format is ddmmmyy:hh:mm:ss. And he wants to see the days, weeks and months of the therapy.
How can I solve this?
Caro
Is your date a actual SAS datetime variable or something character?
You may also want to provide some example of what your data looks like.
The SAS INTCK function will return the number of intervals between two SAS date, time or datetime variables
You could get the number of days between two datetime values with
days = intck('dtday',firstdt, seconddt)
weeks =intck('dtweek',firstdt, seconddt)
months=intck('dtmonth',firstdt, seconddt)
but the values would have to be SAS datetime values.
Or use dates and the interval in the function of 'day' 'week' or 'month'
According to format you have a daytime variably.
You can get the date using function datepart(<daytime variable>);
Having two dates calculate:
- days = date2 - date1;
- months = intck('month', date1, date2);
- weeks can be calculated as days/7 or by using intck('week', date1, date2);
Do it all in one step:
data datdif
set therapy
start = datepart(start);
end = datepart(end);
days = end - start;
weeks = intck('week', start, end);
months = intck('month', start, end);
run;
proc freq data = datdif;
tables days weeks months;
run;
Is your date a actual SAS datetime variable or something character?
You may also want to provide some example of what your data looks like.
The SAS INTCK function will return the number of intervals between two SAS date, time or datetime variables
You could get the number of days between two datetime values with
days = intck('dtday',firstdt, seconddt)
weeks =intck('dtweek',firstdt, seconddt)
months=intck('dtmonth',firstdt, seconddt)
but the values would have to be SAS datetime values.
Or use dates and the interval in the function of 'day' 'week' or 'month'
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.