SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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