BookmarkSubscribeRSS Feed
SAS_NoobMaster
Calcite | Level 5
data p1;
infile cards missover;
input ID $ Name $ Category $ Price AmountQ1 AmountQ2 AmountQ3 AmountQ4;
cards;
001 SSD Hardware 100 453 500 550 588
018 Cooling Hardware 399 918 1000 1020 1231
003 MAV Display 344 201 222 323 344
015 Boss Audio 213 989 1020 928 999
006 RTX1070 Hardware 399 472 560 832 991
014 Mouse1 Control 121 533 463 758 432
008 GTX1070 Hardware 178 371 333 212 121
009 GTX1060 Hardware 222 432 333 232 211
009 GTX1060 Hardware 222 432 333 232 211
005 RTX1060 Hardware 299 847 919 1029 2910
010 I79700k Hardware 232 141 212 323 432
005 RTX1060 Hardware 299 847 919 1029 2910
011 I79800k Hardware 245 131 121 323 432
013 Vaper Control 123 141 143 145 156
017 LED Display 144 119 100 98 87
002 IPS Display 200 193 199 201 234
012 I92000k Hardware 443 111 131 141 145
016 Logic Control 321 531 542 455 543
004 GTX1080 Hardware 399 876 763 543 312
007 RTX1080 Hardware 569 938 1039 1410 1235
019 Sony Audio 123 98 102 92 99
019 Sony Audio 123 98 102 92 99
;run;
%let aq=amountq1,amountq2,amountq3,amountq4;

I was trying to count the number of product that has a above or below average sales

My idea is to Create variable 'performance' to indicate whether the product is above or below average and use the summary function count to count their number.

 

Using the code below

 

proc sql;
select case when sum(&aq)*price ge mean(sum(&aq)*price) then 'above average'
else 'below average' end as performance, 
count(calculated performance) as cont
from p1 group by calculated performance;
quit;

I get an error of : Summary functions are restricted to the SELECT and HAVING clauses only

 

when I change <mean(sum(&aq)*price)> to an actual number, or use subquery like below the problem is solved.

proc sql;
select case when sales ge avg then 'above average'
else 'below average' end as performance, count(calculated performance) as cont
from (select *,sum(&aq)*price as sales,mean(sum(&aq)*price) as avg from p1) group by calculated performance;
quit;

My question is: Why? I only used summary function in the select clause, why is SAS telling me this?

1 REPLY 1
unison
Lapis Lazuli | Level 10

Invoking a summary function to be calculated in a group by or another summary function is not allowed (giving meaning to the error message):

 

proc sql;
select case when sum(&aq)*price ge mean(sum(&aq)*price) then 'above average'
else 'below average' end as performance, 
count(calculated performance) as cont /*not allowed*/
from p1 group by calculated performance /*not allowed*/
;
quit;

 

Whereas in this case "calculated performance" is calculating a case statement -- not a summary function.

proc sql;
select case when sales ge avg then 'above average'
else 'below average' end as performance, count(calculated performance) as cont /*allowed - performance is a case statement*/
from (select *,sum(&aq)*price as sales,mean(sum(&aq)*price) as avg from p1) group by calculated performance /*allowed - performance is a case statement*/;
quit;

 

This is why your first query runs when commenting out the group by statement and the count(calculated performance) line.

-unison

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 475 views
  • 0 likes
  • 2 in conversation