Published on :
ETL CREATION_INTERNE

Calculating Grades and Averages for Students

This code is also available in: Deutsch Español Français
Awaiting validation
The script consists of three pairs of DATA STEP / PROC PRINT blocks. The first block (Grades1) calculates an average in two ways to highlight missing value handling, then assigns a grade. The second block (Grades2) shows an alternative syntax for range comparisons. The third block (Grades3) demonstrates the use of logical operators AND (&) and OR (|) to modify a calculation. Each created table is then displayed with PROC PRINT.
Data Analysis

Type : CREATION_INTERNE


Student data is integrated directly into each DATA step via the 'DATALINES' statement, making the script self-contained.

1 Code Block
DATA STEP Data
Explanation :
This block creates the 'Grades1' table. It reads student data. 'actualavg' is calculated with the MEAN function, which ignores missing values. 'avgscore' is calculated manually, which propagates missing values (the result will be missing if a grade is missing). A series of IF/ELSE IF conditions assigns a textual grade to the 'result' variable based on 'avgscore'.
Copied!
1DATA Grades1;
2 LENGTH RESULT $12.;
3 INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
4 actualavg = mean(exam1,exam2,exam3,exam4,exam5,project,finalexam);
5 avgscore = (exam1+exam2+exam3+exam4+exam5+project+finalexam)/7;
6
7 IF avgscore=. THEN RESULT='Invalid'; * missing value;
8 ELSE IF avgscore >= 90 THEN RESULT='A+';
9 ELSE IF avgscore >= 88 AND avgscore < 90 THEN RESULT='A';
10 ELSE IF avgscore >= 86 AND avgscore < 88 THEN RESULT='B';
11 ELSE IF avgscore >= 84 AND avgscore < 86 THEN RESULT='C';
12 ELSE IF avgscore < 84 THEN RESULT='F';
13
14 DATALINES;
151011 Alia Bhatt 2 100 65 83 84 99 91 96
161012 Maria Smith 1 78 82 86 . 100 95 97
171111 Thomas Jones 2 88 81 96 69 91 90 98
181121 Benedictine Arnold 2 68 82 82 89 89 93 99
191301 Trisha Gupta 1 51 69 79 59 85 64 100
20;
21RUN;
2 Code Block
PROC PRINT
Explanation :
This procedure displays the 'name', 'actualavg', 'avgscore', and 'result' columns from the 'Grades1' table to visualize the results of the first DATA STEP.
Copied!
1 
2PROC PRINT
3DATA=Grades1;
4var name actualavg avgscore RESULT;
5RUN;
6 
3 Code Block
DATA STEP Data
Explanation :
This block creates the 'Grades2' table. It is similar to the first but uses an alternative syntax for range conditions (e.g., '90<=x<=100') to assign the grade. It uses an intermediate variable 'x' for the average calculation.
Copied!
1DATA Grades2;
2 LENGTH RESULT $12.;
3 INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
4 actualavg = mean(exam1,exam2,exam3,exam4,exam5,project,finalexam);
5 x = (exam1+exam2+exam3+exam4+exam5+project+finalexam)/7;
6
7 IF x=. THEN RESULT='Invalid'; * missing value;
8 ELSE IF 90<=x<=100 THEN RESULT='A+';
9 ELSE IF 88<=x<90 THEN RESULT='A';
10 ELSE IF 86<=x<88 THEN RESULT='B';
11 ELSE IF 84<=x<86 THEN RESULT='C';
12 ELSE IF 0<=x<84 THEN RESULT='F';
13
14 avgscore = x;
15
16 DATALINES;
171011 Alia Bhatt 2 100 65 83 84 99 91 96
181012 Maria Smith 1 78 82 86 . 100 95 97
191111 Thomas Jones 2 88 81 96 69 91 90 98
201121 Benedictine Arnold 2 68 82 82 89 89 93 99
211301 Trisha Gupta 1 51 69 79 59 85 64 100
22;
23RUN;
4 Code Block
PROC PRINT
Explanation :
This procedure displays the 'name', 'actualavg', 'avgscore', and 'result' columns from the 'Grades2' table.
Copied!
1 
2PROC PRINT
3DATA=Grades2;
4var name actualavg avgscore RESULT;
5RUN;
6 
5 Code Block
DATA STEP Data
Explanation :
This block creates the 'Grades3' table. It calculates an average 'x' rounded to one decimal place. Then, it uses a complex condition with the logical operators 'AND' (& or and) and 'OR' (|) to create a new variable 'y'. 'y' is incremented by 1 if the first three exam scores are increasing OR if exam scores 3 to 5 are increasing.
Copied!
1DATA Grades3;
2 INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
3 x = round(((exam1+exam2+exam3+exam4+exam5+project+finalexam)/7),1);
4
5 IF (exam1
6 THEN y=x+1;
7 ELSE y=x;
8 
9 DATALINES;
101011 Alia Bhatt 2 100 65 83 84 99 91 96
111012 Maria Smith 1 78 82 86 . 100 95 97
121111 Thomas Jones 2 88 81 96 69 91 90 98
131121 Benedictine Arnold 2 68 82 82 89 89 93 99
141301 Trisha Gupta 1 51 69 79 59 85 64 100
15;
16RUN;
6 Code Block
PROC PRINT
Explanation :
This procedure displays the grades, the calculated average 'x', and the conditional value 'y' from the 'Grades3' table to illustrate the effect of logical operators.
Copied!
1 
2PROC PRINT
3DATA=Grades3;
4var name exam1 exam2 exam3 exam4 exam5 x y;
5RUN;
6 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.