Data is generated directly in the code using the DATALINES statement.
1 Code Block
DATA STEP Data
Explanation : Creation of the Grades1 table. WARNING: The 'result' variable is initialized to '' (empty string), which sets its length to 1 character. The values 'Failed' and 'Passed' will be truncated to 'F' and 'P'.
Copied!
DATA Grades1;
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=''; * missing value;
else if avgscore < 80 then result='Failed';
else result='Passed';
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
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
Explanation : Display of Grades1 to observe the truncation of the 'result' variable.
Copied!
PROC PRINT DATA=Grades1;
var name avgscore actualavg result;
RUN;
1
2
PROC PRINT
3
DATA=Grades1;
4
var name avgscore actualavg RESULT;
5
RUN;
6
3 Code Block
DATA STEP Data
Explanation : Creation of Grades2. Here, 'result' is initialized with a 9-space string. This implicitly forces the length to 9, avoiding truncation, but consumes unnecessary space if the string is empty.
Copied!
DATA Grades2;
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=' '; * missing value;
else if avgscore < 80 then result='Failed';
else result='Passed';
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
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
Explanation : Display of Grades2. The values 'Failed' and 'Passed' appear correctly.
Copied!
PROC PRINT DATA=Grades2;
var name avgscore actualavg result;
RUN;
1
2
PROC PRINT
3
DATA=Grades2;
4
var name avgscore actualavg RESULT;
5
RUN;
6
5 Code Block
DATA STEP Data
Explanation : Creation of Grades3. Using the 'LENGTH result $8.;' statement before using the variable. This is the recommended method for precisely controlling the type and length of variables.
Copied!
DATA Grades3;
length result $8.;
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=''; * missing value;
else if avgscore < 80 then result='Failed';
else result='Passed';
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
LENGTHRESULT $8.;
3
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
Explanation : Final display of Grades3 confirming correct variable handling.
Copied!
PROC PRINT DATA=Grades3;
var name avgscore actualavg result;
RUN;
1
2
PROC PRINT
3
DATA=Grades3;
4
var name avgscore actualavg RESULT;
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.