Obsidian | Level 7

## Count Summary by time (Hours)

Hi There.

I have a large data set and the records have a Time variable in the format Time8.

What i'm trying to do is do a summary count by hour, so if the time is 0:07:01 then that would be in the hour summary of 0:00:00 to 1:00:00

and i'm not sure of the best way to do it.

I've tried using an intck function but have been unsuccessful so far.

Stret

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Count Summary by time (Hours)

Create a new variable that normalizes your data to the hour, using the fact that SAS times are counts of seconds:

``````data have;
input mytime time8.;
format mytime time8.;
cards;
11:32:44
12:33:22
12:45:56
13:16:08
13:25:00
;
run;

data have1;
set have;
hours = int(mytime/3600)*3600;
format hours time8.;
run;

proc summary data=have1 n nway;
class hours;
output out=want (drop=_:) n(mytime)=count;
run;

``````
4 REPLIES 4
Super User

## Re: Count Summary by time (Hours)

Create a new variable that normalizes your data to the hour, using the fact that SAS times are counts of seconds:

``````data have;
input mytime time8.;
format mytime time8.;
cards;
11:32:44
12:33:22
12:45:56
13:16:08
13:25:00
;
run;

data have1;
set have;
hours = int(mytime/3600)*3600;
format hours time8.;
run;

proc summary data=have1 n nway;
class hours;
output out=want (drop=_:) n(mytime)=count;
run;

``````
Diamond | Level 26

## Re: Count Summary by time (Hours)

What part are you unsure about?  To me the problem is a simple one:

1) Create a variable called hour, and window your times into a categorical variable

2) Do you calculations based on the cat hour

For example:

```data want;
set have;
hr=hour(yourtime) + 1;
run;

proc means data=want;
by hour;
...
```

So the hour function returns the hour from a time variable, which will be 0 for everything up to 1:00:00, then 1 etc. So add 1 to that.  If thats not exactly what you want then maybe a select clause for each condition.

Obsidian | Level 7

## Re: Count Summary by time (Hours)

I think I was overthinking it.

Thank you both for the responses;

Super User

## Re: Count Summary by time (Hours)

With date, time and datetime values sometimes all you need is a careful choice of format:

```data have;
input mytime time8.;
format mytime time8.;
cards;
11:32:44
12:33:22
12:45:56
13:16:08
13:25:00
00:07:15
04:21:09
;
run;

proc summary data=have n nway;
class mytime;
format mytime  hhmm2.;
output out=want (drop=_:) n(mytime)=count;
run;

```

Note that the resulting values of mytime in the want set are still time values, but the format displays only the hour portion. The actual value will usually be the smallest in the interval as "rounded" by the format.

Discussion stats
• 4 replies
• 1268 views
• 2 likes
• 4 in conversation