Help using Base SAS procedures

Count Summary by time (Hours)

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

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.

 

Thanks in advance 

 

Stret


Accepted Solutions
Solution
‎10-18-2017 04:42 AM
Super User
Posts: 10,280

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎10-18-2017 04:42 AM
Super User
Posts: 10,280

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,599

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.  

Contributor
Posts: 66

Re: Count Summary by time (Hours)

I think I was overthinking it.

 

Thank you both for the responses;

 

 

Super User
Posts: 13,583

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.

 

☑ This topic is solved.

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

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