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!
DATA Grades1;
length result $12.;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
actualavg = mean(exam1,exam2,exam3,exam4,exam5,project,finalexam);
avgscore = (exam1+exam2+exam3+exam4+exam5+project+finalexam)/7;
if avgscore=. then result='Invalid'; * missing value;
else if avgscore >= 90 then result='A+';
else if avgscore >= 88 AND avgscore < 90 then result='A';
else if avgscore >= 86 AND avgscore < 88 then result='B';
else if avgscore >= 84 AND avgscore < 86 then result='C';
else if avgscore < 84 then result='F';
DATALINES;
1011 Alia Bhatt 2 100 65 83 84 99 91 96
1012 Maria Smith 1 78 82 86 . 100 95 97
1111 Thomas Jones 2 88 81 96 69 91 90 98
1121 Benedictine Arnold 2 68 82 82 89 89 93 99
1301 Trisha Gupta 1 51 69 79 59 85 64 100
;
RUN;
1
DATA Grades1;
2
LENGTHRESULT $12.;
3
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
IF avgscore=. THENRESULT='Invalid'; * missing value;
8
ELSEIF avgscore >= 90THENRESULT='A+';
9
ELSEIF avgscore >= 88 AND avgscore < 90THENRESULT='A';
10
ELSEIF avgscore >= 86 AND avgscore < 88THENRESULT='B';
11
ELSEIF avgscore >= 84 AND avgscore < 86THENRESULT='C';
12
ELSEIF avgscore < 84THENRESULT='F';
13
14
DATALINES;
15
1011 Alia Bhatt 2100658384999196
16
1012 Maria Smith 1788286 . 1009597
17
1111 Thomas Jones 288819669919098
18
1121 Benedictine Arnold 268828289899399
19
1301 Trisha Gupta 1516979598564100
20
;
21
RUN;
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!
PROC PRINT DATA=Grades1;
var name actualavg avgscore result;
RUN;
1
2
PROC PRINT
3
DATA=Grades1;
4
var name actualavg avgscore RESULT;
5
RUN;
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!
DATA Grades2;
length result $12.;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
actualavg = mean(exam1,exam2,exam3,exam4,exam5,project,finalexam);
x = (exam1+exam2+exam3+exam4+exam5+project+finalexam)/7;
if x=. then result='Invalid'; * missing value;
else if 90<=x<=100 then result='A+';
else if 88<=x<90 then result='A';
else if 86<=x<88 then result='B';
else if 84<=x<86 then result='C';
else if 0<=x<84 then result='F';
avgscore = x;
DATALINES;
1011 Alia Bhatt 2 100 65 83 84 99 91 96
1012 Maria Smith 1 78 82 86 . 100 95 97
1111 Thomas Jones 2 88 81 96 69 91 90 98
1121 Benedictine Arnold 2 68 82 82 89 89 93 99
1301 Trisha Gupta 1 51 69 79 59 85 64 100
;
RUN;
1
DATA Grades2;
2
LENGTHRESULT $12.;
3
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
x = (exam1+exam2+exam3+exam4+exam5+project+finalexam)/7;
6
7
IF x=. THENRESULT='Invalid'; * missing value;
8
ELSEIF90<=x<=100THENRESULT='A+';
9
ELSEIF88<=x<90THENRESULT='A';
10
ELSEIF86<=x<88THENRESULT='B';
11
ELSEIF84<=x<86THENRESULT='C';
12
ELSEIF 0<=x<84THENRESULT='F';
13
14
avgscore = x;
15
16
DATALINES;
17
1011 Alia Bhatt 2100658384999196
18
1012 Maria Smith 1788286 . 1009597
19
1111 Thomas Jones 288819669919098
20
1121 Benedictine Arnold 268828289899399
21
1301 Trisha Gupta 1516979598564100
22
;
23
RUN;
4 Code Block
PROC PRINT
Explanation : This procedure displays the 'name', 'actualavg', 'avgscore', and 'result' columns from the 'Grades2' table.
Copied!
PROC PRINT DATA=Grades2;
var name actualavg avgscore result;
RUN;
1
2
PROC PRINT
3
DATA=Grades2;
4
var name actualavg avgscore RESULT;
5
RUN;
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!
DATA Grades3;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
x = round(((exam1+exam2+exam3+exam4+exam5+project+finalexam)/7),1);
if (exam1<exam2 & exam2<exam3) | (exam3<exam4 and exam4<exam5)
then y=x+1;
else y=x;
DATALINES;
1011 Alia Bhatt 2 100 65 83 84 99 91 96
1012 Maria Smith 1 78 82 86 . 100 95 97
1111 Thomas Jones 2 88 81 96 69 91 90 98
1121 Benedictine Arnold 2 68 82 82 89 89 93 99
1301 Trisha Gupta 1 51 69 79 59 85 64 100
;
RUN;
1
DATA 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;
10
1011 Alia Bhatt 2100658384999196
11
1012 Maria Smith 1788286 . 1009597
12
1111 Thomas Jones 288819669919098
13
1121 Benedictine Arnold 268828289899399
14
1301 Trisha Gupta 1516979598564100
15
;
16
RUN;
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!
PROC PRINT DATA=Grades3;
var name exam1 exam2 exam3 exam4 exam5 x y;
RUN;
1
2
PROC PRINT
3
DATA=Grades3;
4
var name exam1 exam2 exam3 exam4 exam5 x y;
5
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.