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
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;
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;
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.
I think I was overthinking it.
Thank you both for the responses;
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.