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

Hello,

I need someone to help me with recoding my data.

I have 4 variables (MAT, BIO, PHY, CHE). Each of the variable has students scores from 1 - 9, with 9 being the worst score. For example a student in my dataset has a score of 2 in MAT, 3 in BIO, 4 in PHY and 1 in CHE.

What I want to do is create an aggregate variable from the four subjects using the three best grades. In this example, the three best scores are MAT (2), BIO (3) and CHE (1).

Thus, the new variable, which will be called Aggregate Varaible will  AGGREG_VAR = (2+3+1) = 6.

So the score for this student for AGGREG_VAR = 6.

 

PLEASE I NEED HELP IMPLEMENTING THIS IS SAS.

 

THANK YOU.

 

FRANCIS

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20
  AGGREG_VAR =sum(MAT,BIO,PHY,CHE)-(nmiss(MAT,BIO,PHY,CHE)=0)*max(MAT,BIO,PHY,CHE);

if missing values can exist. 🙂

View solution in original post

7 REPLIES 7
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
  MAT=4 ;BIO=3; PHY=2 ;CHE=1;
  AGGREG_VAR =sum( MAT,BIO,PHY,CHE)-max( MAT,BIO,PHY,CHE);
run;

AGGREG_VAR=6

 

Reeza
Super User

Can you have missing values?

 

ChrisNZ
Tourmaline | Level 20
  AGGREG_VAR =sum(MAT,BIO,PHY,CHE)-(nmiss(MAT,BIO,PHY,CHE)=0)*max(MAT,BIO,PHY,CHE);

if missing values can exist. 🙂

nhyira
Calcite | Level 5
Hi ChrisNZ,
You're are amazing, and a savior! Thank you. Thank you!!
Francis
ChrisNZ
Tourmaline | Level 20

You're welcome. Don't forget to finalise the thread by setting the "solution found" flag.

nhyira
Calcite | Level 5

Hi,

Yes I can have missing values, and there is also the possibility of a student having for example a score of 3 in all subjects, which in this case the maximum will be 3. 

 

Thank you for your help!

 

Francis

Reeza
Super User

So a student with two scores would have a lower aggregate than a student with all 4 scores.

@ChrisNZ solution accounts for missing values. If a student who has two missing values is the same as a student who has scores for all 4 then this is fine. 

 

This is a methodological consideration, not an issue with the code. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 2260 views
  • 0 likes
  • 3 in conversation