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

Hello! I have a data set that looks like this:

 

ID H1 H2 H3 H4 H5 H6 H7 H8 H9

01 10 10 10 10 10 10 10 10 10

02 10 8 5 10 10 0 9 10 3

03 9 10 8 10 9 10 10 10 6

04 7 10 7 8 10 0 10 0 10

05 5 10 6 10 4 10 10 9 10

 

I need to drop the lowest 2 homework scores and create a variable for the overall homework score for a student. So say for student ID 04, the two zero grades would be dropped and a percentage for homework would be (7+10+7+8+10+10+10)/70 = 88.6 % (homework scores are out of 10.

 

Ive tried commands like this in the data step:

 

oops1= min(of Q1--Q9);
if H1=min(of Q1--Q9) then drop H1;

But I'm not sure where to go from here. Based on my research I think I might need to build an array? Hopefully not cause I have very little experience with that. Any advice would be greatly appreciated!

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data want;
    set have;
    call sortn(of h1-h9);
    average = mean(of h3-h9);
run;

 

CALL SORTN sorts H1-H9 in ascending order, thus H3-H9 are now the 7 largest (doesn't contain the two lowest) and you can average those.

 

I'm not sure what this will do if there are missing values in H1 through H9.

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
/* UNTESTED CODE */
data want;
    set have;
    call sortn(of h1-h9);
    average = mean(of h3-h9);
run;

 

CALL SORTN sorts H1-H9 in ascending order, thus H3-H9 are now the 7 largest (doesn't contain the two lowest) and you can average those.

 

I'm not sure what this will do if there are missing values in H1 through H9.

--
Paige Miller
valarievil
Obsidian | Level 7

On of my lines is:
5 9 0 0 0 0 0 0 0
for HW 1-9. If the code worked as it should, I should get 2 as the output for average right? (5+9+0+0+0+0+0)/7=2. Instead I got 1.3571. Is my math wrong or is the code wrong?

 

NEVERMIND! I just had to change some variable names. ALL GOOD THANK YOU!

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!

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
  • 2 replies
  • 944 views
  • 3 likes
  • 2 in conversation