Help using Base SAS procedures

DaYs calculation

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,040
Accepted Solution

DaYs calculation

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


Accepted Solutions
Solution
‎05-23-2013 07:09 PM
Super User
Super User
Posts: 6,502

Re: DaYs calculation

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


All Replies
Respected Advisor
Posts: 4,654

Re: DaYs calculation

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
Super Contributor
Posts: 1,040

Re: DaYs calculation

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

Super User
Posts: 17,868

Re: DaYs calculation

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

Super Contributor
Posts: 1,040

Re: DaYs calculation

Hi ,

I did not understand it clearly.

Could you please explain with the code?

Thanks

Solution
‎05-23-2013 07:09 PM
Super User
Super User
Posts: 6,502

Re: DaYs calculation

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


Super Contributor
Posts: 1,040

Re: DaYs calculation

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

Super User
Super User
Posts: 6,502

Re: DaYs calculation

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 265 views
  • 6 likes
  • 4 in conversation