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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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