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

I'm looking to recreate the averageif function from Excel.  I have the following columns:  Date, Hour, Peak, Price.  Peak is boolean (0 or 1).  Sample dataset:

 

DateHEPeakPriceAvg
1/4/20171028.4830.84
1/4/20172028.1530.84
1/4/20173027.6130.84
1/4/20174026.6130.84
1/4/20175028.1430.84
1/4/20176036.1630.84
1/4/20177141.3239.05
1/4/20178145.4039.05
1/4/20179139.0139.05
1/4/201710134.9339.05
1/4/201711133.6239.05
1/4/201712131.7039.05
1/4/201713126.3339.05
1/4/201714129.5439.05
1/4/201715132.5139.05
1/4/201716137.2739.05
1/4/201717141.9639.05
1/4/201718151.8239.05
1/4/201719151.5339.05
1/4/201720145.5239.05
1/4/201721142.7239.05
1/4/201722139.6939.05
1/4/201723036.5330.84
1/4/201724035.0430.84

 

I want to find the average (Avg) for each day by Peak type.  Some days could have all zeros for Peak.  Looking to do this in a data step if possible.  Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data have;
input Date :mmddyy10.	HE	Peak	Price;
format date mmddyy10.;
datalines;
1/4/2017	1	0	28.48
1/4/2017	2	0	28.15
1/4/2017	3	0	27.61
1/4/2017	4	0	26.61
1/4/2017	5	0	28.14
1/4/2017	6	0	36.16
1/4/2017	7	1	41.32
1/4/2017	8	1	45.40
1/4/2017	9	1	39.01
1/4/2017	10	1	34.93
1/4/2017	11	1	33.62
1/4/2017	12	1	31.70
1/4/2017	13	1	26.33
1/4/2017	14	1	29.54
1/4/2017	15	1	32.51
1/4/2017	16	1	37.27
1/4/2017	17	1	41.96
1/4/2017	18	1	51.82
1/4/2017	19	1	51.53
1/4/2017	20	1	45.52
1/4/2017	21	1	42.72
1/4/2017	22	1	39.69
1/4/2017	23	0	36.53
1/4/2017	24	0	35.04
;



proc sql;
   create table want as
   select *,
          mean(Price) as Avg
   from have
   group by Peak
   order by HE;
quit;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20
data have;
input Date :mmddyy10.	HE	Peak	Price;
format date mmddyy10.;
datalines;
1/4/2017	1	0	28.48
1/4/2017	2	0	28.15
1/4/2017	3	0	27.61
1/4/2017	4	0	26.61
1/4/2017	5	0	28.14
1/4/2017	6	0	36.16
1/4/2017	7	1	41.32
1/4/2017	8	1	45.40
1/4/2017	9	1	39.01
1/4/2017	10	1	34.93
1/4/2017	11	1	33.62
1/4/2017	12	1	31.70
1/4/2017	13	1	26.33
1/4/2017	14	1	29.54
1/4/2017	15	1	32.51
1/4/2017	16	1	37.27
1/4/2017	17	1	41.96
1/4/2017	18	1	51.82
1/4/2017	19	1	51.53
1/4/2017	20	1	45.52
1/4/2017	21	1	42.72
1/4/2017	22	1	39.69
1/4/2017	23	0	36.53
1/4/2017	24	0	35.04
;



proc sql;
   create table want as
   select *,
          mean(Price) as Avg
   from have
   group by Peak
   order by HE;
quit;
diego4444
Calcite | Level 5

So much easier in proc sql then forcing myself to do in data step. Thanks for your help! I ended up using this:

proc sql;
select *,
mean(price) as Avg
from temp_new
group by Date, Peak
order by Date, Hour;
quit;

PeterClemmensen
Tourmaline | Level 20

The right tool for the right job 🙂 Glad you found your answer.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1934 views
  • 0 likes
  • 2 in conversation