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

I have been given the following data called P_sort: (small portion of it)
assoc_count  ...    time
5                           18JUL2017:23:59:59.0000000
9                           19JUL2017:02:59:59.0000000
2                           19JUL2017:05:59:59.0000000
1                           19JUL2017:08:59:59.0000000
2                           19JUL2017:11:59:59.0000000
1                           19JUL2017:14:59:59.0000000
4                           19JUL2017:17:59:59.0000000
9                           19JUL2017:20:59:59.0000000
10                         19JUL2017:23:59:59.0000000
15                         20JUL2017:02:59:59.0000000

...                          ...

 

I am tasked with finding an average of the assoc_count per hour over a specified number of weeks. This average will then be displayed with bar charts.

 

My current code is:

proc TIMESERIES data=P_sort out=P_sort;
   id time interval=hour
   accumulate=total
   start='018jul2018:00:00:00'dt
   end  ='025jul2018:00:00:00'dt;
   var assoc_count;
run;

/* AVERAGING GOES HERE*/

proc gchart data=P_sort;
	vbar time / sumvar=assoc_count maxis=axis1 raxis=axis2;
run;

 I don't know how to implement this. Any suggestions would be helpful.

1 ACCEPTED SOLUTION

Accepted Solutions
acloseCC
Fluorite | Level 6

My final solution is:

 

DATA P_sort;
	set L***;
	hour = hour(time);
run;

proc sql;
	create table selected_P_sort as
	select assoc_count, hour 
	from P_sort
	where datepart(time) between '01APR2018'd and '15APR2018'd AND hour between 7 and 23;
quit;

proc summary data=selected_P_sort;
	var assoc_count;
	class hour;
	output out=fin_P_sort MEAN=;
run;

 Thanks PaigeMiller and SASKiwi.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

In a data step, compute the hour of the data.

 

hour = hour(datepart(time));

Then, run PROC SUMMARY on this data set to compute the averages by hour.

--
Paige Miller
acloseCC
Fluorite | Level 6

What would the "PROC SUMMARY" look like?

PaigeMiller
Diamond | Level 26

There are examples in the PROC SUMMARY documentation. Give it a try. 

--
Paige Miller
acloseCC
Fluorite | Level 6

When I use the following code, hour in the table does not change (stays at 185).

DATA P_sort;
	set LIB.W_W_HOURLY;
	hour = hour(datepart(time));
run;

How can this be fixed? I apologize for my limited knowledge of SAS. 

SASKiwi
PROC Star

Using DATEPART isn't necessary and it will give you the wrong answer. I think you can use it directly like this:

DATA P_sort;
	set LIB.W_W_HOURLY;
	hour = hour(time);
run;
acloseCC
Fluorite | Level 6

My final solution is:

 

DATA P_sort;
	set L***;
	hour = hour(time);
run;

proc sql;
	create table selected_P_sort as
	select assoc_count, hour 
	from P_sort
	where datepart(time) between '01APR2018'd and '15APR2018'd AND hour between 7 and 23;
quit;

proc summary data=selected_P_sort;
	var assoc_count;
	class hour;
	output out=fin_P_sort MEAN=;
run;

 Thanks PaigeMiller and SASKiwi.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 6 replies
  • 2079 views
  • 0 likes
  • 3 in conversation