BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Caro17
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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'

View solution in original post

4 REPLIES 4
Shmuel
Garnet | Level 18

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);

Caro17
Calcite | Level 5
With the days I get between 0 and 31536000 days. That means there must be also weeks and months. But I get nothing with weeks and months.

data datdif
set therapy
start = datepart(start);
end = datepart(end);
run;

data datdif;
set therapy
days = end - start;
run;

proc freq data = datdif; tables days; run;

data datdif;
set therapy;
weeks = intck('week', start, end);

proc freq data = datdif; tables weeks; run;

data datdif;
set therapy;
months = intck('month', start, end);

proc freq data = datdif; tables months; run;
Shmuel
Garnet | Level 18

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;
ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1096 views
  • 1 like
  • 3 in conversation