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
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.
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
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
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
Subtract your dates and then format them the way you want.
Hi ,
I did not understand it clearly.
Could you please explain with the code?
Thanks
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.
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
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
There are 24*60*60 seconds in a day. So divide DURATION in my example by that number and you will have fractional days.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.