BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Stretlow
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

Stretlow
Obsidian | Level 7

I think I was overthinking it.

 

Thank you both for the responses;

 

 

ballardw
Super User

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.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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