turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- DaYs calculation

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 04:40 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 07:09 PM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 05:18 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 05:35 PM

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**

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 05:46 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 06:43 PM

Hi ,

I did not understand it clearly.

Could you please explain with the code?

Thanks

Solution

05-23-2013
07:09 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 07:09 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 07:22 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-23-2013 08:17 PM

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