How many hours each month

Solved
Occasional Contributor
Posts: 11

How many hours each month

I received a request where the customer wants me to show how many hours the patient stayed in each month.

Here is an exampleatient was here January 1st 2:00 pm and discharge February 2nd 1:00AM

Should become like this:

January: 730 hours (10 hours on January 1st + 30 days *24 hours per day)

February 25 hours (24 hours on February 1st and 1 hour on February 2nd).

If you cannot figure out a way, I would be greatly appreciated if you can at least provide me information how to calculate the number of hours patient stayed on first and last day (10 hours on January 1st and 1 hour on February 2nd). I have a way to calculate the hours between January 2nd  and February 1st. I am having problems in getting the hours to midnight for first day and midnight to discharge datetime for the last day

I tried intck with 'end', 'b' but I keep getting error messages.

Here is sample of the checkin and checkout datetime

CheckinDate: 01JAN2014:2:00:00PM

CheckoutDate: 02FEB2014:1:00:00AM

THis is a followup after this post I have posted

This time the requester wants number of hours instead of number of days each month.

Accepted Solutions
Solution
‎05-20-2015 01:59 AM
Super User
Posts: 10,258

Re: How many hours each month

data want (keep=yymm hours);

set have;

start = checkin;

yymm = year(datepart(checkin))*100 + month(datepart(checkin));

yymmend = year(datepart(checkout))*100 + month(datepart(checkout));

do while(yymm < yymmend);

hours = round((intnx('dtmonth',start,0,'end') - start) / 3600,1);

output;

start = intnx('dtmonth',start,1,'begin');

yymm = year(datepart(start))*100 + month(datepart(start));

end;

hours = round((checkout - start) / 3600,1);

output;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 10,784

Re: How many hours each month

data temp;

CheckinDate= '01JAN2014:2:00:00PM'dt;

CheckoutDate= '02FEB2014:1:00:00AM'dt;

i=CheckinDate;

do while(i lt CheckoutDate);

date=datepart(i); output;

i+3600;

end;

run;

proc summary data=temp nway;

class date;

format date monyy7.;

output out=want(drop= _type_ rename=(_freq_=hours)) ;

run;

Xia Keshan

Occasional Contributor
Posts: 11

Re: How many hours each month

Hi thank you for your help Xia.

When I tried with checkin date time April 4, 2014 17:25 and checkout datetime April 30, 2014 23:59 the hours are a bit off.

Using the code above I got 635 hours.

I used this website to do the calculation and it came with 630 hours not sure what they meant by rounding down.

http://www.timeanddate.com/date/durationresult.html?m1=4&d1=4&y1=2014&m2=4&d2=30&y2=2014&h1=17&i1=25...

Here is the timestamp but I don't think it should matter since the area and visit_number is the same.

 Checkin Date/Time Checkout Date/Time
 4/4/2014 17:25 4/14/2014 11:08 4/14/2014 11:08 4/14/2014 11:10 4/14/2014 11:10 4/16/2014 10:44 4/16/2014 10:44 4/21/2014 8:12 4/21/2014 8:12 4/23/2014 14:35 4/23/2014 14:35 4/27/2014 16:47 4/27/2014 16:47 4/29/2014 21:38 4/29/2014 21:38 5/3/2014 0:15
Super User
Posts: 13,554

Re: How many hours each month

If you can reduce the data to one pair of checkin / checkout SAS datetime variables then

Hours = (checkout - checkin)/3600; Which will likely have a decimal component.

Super User
Posts: 10,784

Re: How many hours each month

Your DateTime value is not right .

data temp;

CheckinDate= '04apr2014:17:25:00'dt;

CheckoutDate= '30apr2014:23:59:59'dt;

i=CheckinDate;

do while(i lt CheckoutDate);

date=datepart(i); output;

i+3600;

end;

run;

proc summary data=temp nway;

class date;

format date monyy7.;

output out=want(drop= _type_ rename=(_freq_=hours)) ;

run;

Solution
‎05-20-2015 01:59 AM
Super User
Posts: 10,258

Re: How many hours each month

data want (keep=yymm hours);

set have;

start = checkin;

yymm = year(datepart(checkin))*100 + month(datepart(checkin));

yymmend = year(datepart(checkout))*100 + month(datepart(checkout));

do while(yymm < yymmend);

hours = round((intnx('dtmonth',start,0,'end') - start) / 3600,1);

output;

start = intnx('dtmonth',start,1,'begin');

yymm = year(datepart(start))*100 + month(datepart(start));

end;

hours = round((checkout - start) / 3600,1);

output;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
🔒 This topic is solved and locked.