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

Hi,

I want to calculate the no of Days.

IN and OUT pairs are together(IN1&OUT1  and so on).

ID              IN1                                   OUT1                          IN2                                OUT2             IN3      OUT3        IN4   OUT4

101     03AUG12:02:10:00    03AUG12:21:00:00    08SEP12:19:22:00     11SEP12:09:20:00    -------       ------         -----    ------

102     ----------------------          ----------------------      13DEC11:11:30:00     14DEC11:15:00:00   ------       -------         ------   ------

WANT :

ID          DAYS

101       3days

102       1day and 4hrs(how to write as Days)     

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

What is it you really want to sum?   Parts of Days, Complete Days, Total duration in seconds?

For example in the first pair for ID 101 does that contribute 1 day? 0 days? or just under 19 hours?

Assuming you want to add up the seconds.

  • subtract IN from OUT you will get a duration in seconds.
  • sum over all of the groups you will get total duration in seconds.
  • then use DATEPART() function (or just divide by the number of seconds in a day) to get the number of days.
  • Do you want to display the fractional part of the day in fractional hours? or HH:MM format?

data want(keep=id days);

  set have;

  array in in:;

  array out out:;

  do i = 1 to dim(in);

     if 2=N(in(i),out(i)) then duration=sum(duration,out(i)-in(i));

  end;

  days = datepart(duration);

  time = timepart(duration);

  hours = time /(60*60);

  put days= hours= 5.2  time= time5. ;

run;

days=3 hours=8.80 time=8:48

days=1 hours=3.50 time=3:30


View solution in original post

7 REPLIES 7
PGStats
Opal | Level 21

Use function intck and sum the number of times that midnight is crossed for all in-out pairs, as follows :

data have;
informat in1-in4 out1-out4 datetime16.;
input id in1 out1 in2 out2 in3 out3 in4 out4;
datalines;
101     03AUG12:02:10:00    03AUG12:21:00:00    08SEP12:19:22:00     11SEP12:09:20:00    . . . .
102     .          .      13DEC11:11:30:00     14DEC11:15:00:00   . . . .
;

data want(keep=id days);
set have;
array in{*} in:;
array out{*} out:;
do i = 1 to dim(in);
     if not missing(in{i}) then days = sum(days, intck("DTDAY",in{i},out{i}));
     end;
run;

proc print noobs; run;

PG

PG
robertrao
Quartz | Level 8

Hi PG,

Could you please explain what is happening in this step??

if not missing(in{i}) then days = sum(days, intck("DTDAY",in{i},out{i}));

     why only "not missing in(i)"........why not outs???

Is this program making sure that in1 and out1 go together??sum it?? NEXT in2 out2 sum it and finally overall sum????which step in the code is doing that?

13DEC:11:30  to 14DEc 15:00 is

1day and 3 hrs....those 3 hours also need to be included

Thanks in Advance

Reeza
Super User

Subtract your dates and then format them the way you want.

robertrao
Quartz | Level 8

Hi ,

I did not understand it clearly.

Could you please explain with the code?

Thanks

Tom
Super User Tom
Super User

What is it you really want to sum?   Parts of Days, Complete Days, Total duration in seconds?

For example in the first pair for ID 101 does that contribute 1 day? 0 days? or just under 19 hours?

Assuming you want to add up the seconds.

  • subtract IN from OUT you will get a duration in seconds.
  • sum over all of the groups you will get total duration in seconds.
  • then use DATEPART() function (or just divide by the number of seconds in a day) to get the number of days.
  • Do you want to display the fractional part of the day in fractional hours? or HH:MM format?

data want(keep=id days);

  set have;

  array in in:;

  array out out:;

  do i = 1 to dim(in);

     if 2=N(in(i),out(i)) then duration=sum(duration,out(i)-in(i));

  end;

  days = datepart(duration);

  time = timepart(duration);

  hours = time /(60*60);

  put days= hours= 5.2  time= time5. ;

run;

days=3 hours=8.80 time=8:48

days=1 hours=3.50 time=3:30


robertrao
Quartz | Level 8

Hi Tom,

Thanks for the detailed explanation.

i think  summing up all the pairs across ........i need the days involved....

so if you can give me the total hours across all pairs we can divide by 24 to get the days......

If you can show me how to get the hours alone across all the pairs.that would be great


If the sum across all pairs was: 3days and 12 hours then i want it to show as 3.5days

Thanks

Tom
Super User Tom
Super User

There are 24*60*60 seconds in a day.  So divide DURATION in my example by that number and you will have fractional days.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 1002 views
  • 6 likes
  • 4 in conversation