BookmarkSubscribeRSS Feed
jack_knuettel
Calcite | Level 5

I have this dataset with 6 different hw grades. I ned to find the HW average, but I need the lowest value to be dropped. Here is the code:

 Name HW1 HW2 HW3 HW4 HW5 HW6

Abby95.0085.0069.0093.0081.7085.00
Hans95.5056.5077.5050.0079.2075.00
Beyonce90.0083.0089.0097.0090.8085.00
Camila92.5098.0089.0084.0080.0085.00
Carlos95.0069.0055.0092.0076.70100.00
Darius95.0097.0099.0089.00100.0087.50
Terrance93.0092.5095.0068.5082.0095.00
Evelyn92.5094.00100.0096.00100.00100.00
Gertrude95.0096.00100.0097.0092.50100.00
Alamance93.5084.00100.0091.0072.5085.00
Hiromi85.5094.0085.0084.0097.50100.00
Isla80.0040.0045.0061.7064.0072.50
Jasmine95.0098.00100.0096.00100.00100.00
Julieta75.0082.0090.0074.5080.8082.50
Lavanya85.0079.0085.0096.00100.0082.50
Oliver98.00100.0096.5098.0097.50100.00
Elon83.0098.0087.5096.00100.0075.00
Yvette92.5065.0082.5083.3080.00100.00
5 REPLIES 5
ChrisNZ
Tourmaline | Level 20

Lile this?

 data T;
  HW1=1;
  HW2=2;
  HW3=3;
  HW4=4;
  HW5=5;
  HW6=6;
  MIN=min(of HW1-HW6);
  AVG=mean(ifn(HW1=MIN,.,HW1)
          ,ifn(HW2=MIN,.,HW2)
          ,ifn(HW3=MIN,.,HW3)
          ,ifn(HW4=MIN,.,HW4)
          ,ifn(HW5=MIN,.,HW5)
          ,ifn(HW6=MIN,.,HW6));
run;

 

PaigeMiller
Diamond | Level 26

Method #1:

data want;
    set have;
    avg_without_min = (6*mean(of hw1-hw6) - min(of hw1-hw6))/5;
run;

Method B:

data want;
    set have;
    call sortn(of hw1-hw6);
    avg_without_min=mean(of hw2-hw6);
run;

To @jack_knuettel

What happens if there are two values of HW1-HW6 both with the minimum value (in other words, the two are tied)? Then what?

--
Paige Miller
ChrisNZ
Tourmaline | Level 20

Or:

 

avg_without_min = (sum(of hw1-hw6) - min(of hw1-hw6))/(n(of hw1-hw6)-1);

 

PaigeMiller
Diamond | Level 26

@ChrisNZ wrote:

Or:

 

avg_without_min = (sum(of hw1-hw6) - min(of hw1-hw6))/(n(of hw1-hw6)-1);

Certainly an improvement on my earlier Method #1. Handles missing values properly as well.

--
Paige Miller
Reeza
Super User

One more for fun, but won't scale at all.

 

array HW(6) HW1-HW6;

x = mean(largest(1, of HW(*)),
               largest(2, of HW(*)),
               ...
               largest(5, of HW(*));

Another method would be to transpose the data, sorting it then dropping the first record. 

I'd probably use @ChrisNZ solution in reality though.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 5 replies
  • 1026 views
  • 10 likes
  • 4 in conversation