BookmarkSubscribeRSS Feed
NN
Quartz | Level 8 NN
Quartz | Level 8
Guys,
I have a dataset in the below format which has Intime, Outime, the Begin time of a working day and the End time of working day.
I have to calculate the difference between intime and outtime in hours (Time_taken) but the difference has to be on the basis of the BOD and EOD.
Ex: In the second case you can see that time taken is 10 hours in the first day and 8 hours in the second day. hence total time taken is 18 hours.
If somebody has worked on something like this then kindly help.


data test;
infile datalines delimiter = ',';
input INTIME : datetime19. OUTTIME : datetime19. BOD : $5. EOD : $5. Time_taken : $15.;
format intime : datetime19. OUTTIME : datetime19.;
cards;
01OCT2009:08:00:00, 01OCT2009:18:00:00, 08:00, 18:00, 10 (+10)
01OCT2009:08:00:00, 02OCT2009:16:00:00, 08:00, 18:00, 18 (10 +8)
01OCT2009:08:00:00, 02OCT2009:16:00:00, 08:00, 20:00, 20 (12 + 😎
01OCT2009:10:00:00, 03OCT2009:09:00:00, 08:00, 20:00, 23 (10+12+1)
;
run;
2 REPLIES 2
deleted_user
Not applicable
Hi,

I send you one possible solution to your question.
1) I have changed BOD and EOD format to TIME5.
2) The calculation is do using DO sentence by days

Best regards
Jaroslav

data test;
infile datalines delimiter = ',';
input INTIME : datetime19. OUTTIME : datetime19. BOD : TIME5. EOD : TIME5. Time_taken : $15.;
format intime OUTTIME datetime19. BOD EOD TIME5.;
cards;
01OCT2009:08:00:00, 01OCT2009:18:00:00, 08:00, 18:00, 10 (+10)
01OCT2009:08:00:00, 02OCT2009:16:00:00, 08:00, 18:00, 18 (10 +8)
01OCT2009:08:00:00, 02OCT2009:16:00:00, 08:00, 20:00, 20 (12 + 😎
01OCT2009:10:00:00, 03OCT2009:09:00:00, 08:00, 20:00, 23 (10+12+1)
;
run;

DATA TEST(DROP=DAY);
FORMAT DIFERENCE TIME5.;
RETAIN DIFERENCE;
SET TEST;
DO DAY=DATEPART(INTIME) TO DATEPART(OUTTIME);
IF DAY=DATEPART(INTIME) THEN DIFERENCE=0;
DIFERENCE=DIFERENCE+MIN(OUTTIME,DHMS(DAY,0,0,EOD))-MAX(INTIME,DHMS(DAY,0,0,BOD));
IF DAY=DATEPART(OUTTIME) THEN OUTPUT;
END;
RUN;
NN
Quartz | Level 8 NN
Quartz | Level 8
Jaroslav Thanks a lot This works just fine

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 862 views
  • 0 likes
  • 2 in conversation