BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
kz_
Quartz | Level 8 kz_
Quartz | Level 8

I need to calculate the difference between two datetimes in hours, but I need to exclude Sundays and the following holidays: 

1. New Year's Day 

2. Memorial Day 

3. July 4 

4. Labor Day 

5. Thanksgiving 

6. Christmas  

 

For example, using INTCK, I get column 3. I want column 4. I know there is a weekday function, but I don't know how to use it and include Saturdays.

Thanks in advance. 

 

second_time first_time intck('dthour', first_time, second_time) WANT Why? 
04MAR24:12:26:00 01MAR24:11:30:00 73 49 Exclude March 3 (Sunday)
28MAY24:10:54:00 21MAY24:21:15:00 157 109 Exclude May 26 (Sunday) and May 27 (Memorial Day)
01MAR24:09:53:00 01MAR24:09:15:00 0 0 or 1  SAS is rounding down, but up is ok too
         
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*
You need the help of function WEEKDAY() and HOLIDAY().
*/
data have;
infile cards expandtabs truncover;
input (second_time	first_time) (: datetime32.);
format second_time	first_time datetime.;
seconds=0;
do temp=first_time to second_time;
 if weekday(datepart(temp)) ne 1 and
    datepart(temp) ne holiday('NEWYEAR',year(datepart(temp))) and
	datepart(temp) ne holiday('MEMORIAL',year(datepart(temp))) and
    datepart(temp) ne holiday('USINDEPENDENCE',year(datepart(temp))) and
	datepart(temp) ne holiday('LABOR',year(datepart(temp))) and
	datepart(temp) ne holiday('THANKSGIVING',year(datepart(temp))) and
	datepart(temp) ne holiday('CHRISTMAS',year(datepart(temp))) then seconds+1;
end;
want=seconds/'01:00:00't;
drop temp seconds;
cards;
04MAR24:12:26:00	01MAR24:11:30:00	
28MAY24:10:54:00	21MAY24:21:15:00
;

View solution in original post

4 REPLIES 4
Patrick
Opal | Level 21

You have to create a custom interval.

There is sample code in the docu that's already close to what you need: example uses custom intervals in the time function INTCK to omit holidays when counting business day...

Ksharp
Super User
/*
You need the help of function WEEKDAY() and HOLIDAY().
*/
data have;
infile cards expandtabs truncover;
input (second_time	first_time) (: datetime32.);
format second_time	first_time datetime.;
seconds=0;
do temp=first_time to second_time;
 if weekday(datepart(temp)) ne 1 and
    datepart(temp) ne holiday('NEWYEAR',year(datepart(temp))) and
	datepart(temp) ne holiday('MEMORIAL',year(datepart(temp))) and
    datepart(temp) ne holiday('USINDEPENDENCE',year(datepart(temp))) and
	datepart(temp) ne holiday('LABOR',year(datepart(temp))) and
	datepart(temp) ne holiday('THANKSGIVING',year(datepart(temp))) and
	datepart(temp) ne holiday('CHRISTMAS',year(datepart(temp))) then seconds+1;
end;
want=seconds/'01:00:00't;
drop temp seconds;
cards;
04MAR24:12:26:00	01MAR24:11:30:00	
28MAY24:10:54:00	21MAY24:21:15:00
;
kz_
Quartz | Level 8 kz_
Quartz | Level 8

This works perfectly. However, for a big data set, it is impossibly slow. Is there are way for the do loop to ititerate through minutes or hours rather than seconds?

ballardw
Super User

Since datetimes are numbers of seconds change any increment to a multiple you like:

do temp=first_time to second_time by 60;
 if weekday(datepart(temp)) ne 1 and
    datepart(temp) ne holiday('NEWYEAR',year(datepart(temp))) and
	datepart(temp) ne holiday('MEMORIAL',year(datepart(temp))) and
    datepart(temp) ne holiday('USINDEPENDENCE',year(datepart(temp))) and
	datepart(temp) ne holiday('LABOR',year(datepart(temp))) and
	datepart(temp) ne holiday('THANKSGIVING',year(datepart(temp))) and
	datepart(temp) ne holiday('CHRISTMAS',year(datepart(temp))) then seconds+60;
end;

increments by 60 seconds or one minute

 

do temp=first_time to second_time by 3600;
 if weekday(datepart(temp)) ne 1 and
    datepart(temp) ne holiday('NEWYEAR',year(datepart(temp))) and
	datepart(temp) ne holiday('MEMORIAL',year(datepart(temp))) and
    datepart(temp) ne holiday('USINDEPENDENCE',year(datepart(temp))) and
	datepart(temp) ne holiday('LABOR',year(datepart(temp))) and
	datepart(temp) ne holiday('THANKSGIVING',year(datepart(temp))) and
	datepart(temp) ne holiday('CHRISTMAS',year(datepart(temp))) then seconds+3600;
end;

increment by 3600 seconds or 1 hour.

24*3600= 24 hours (1 day).

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 451 views
  • 4 likes
  • 4 in conversation