The script is divided into five logical sections, each creating and displaying a data table (Grades1 to Grades5). The first section shows simple creation and display. Subsequent sections introduce the use of the VAR statement in PROC PRINT to select and order columns. One section demonstrates how to calculate a new variable (totalscores) within a DATA STEP. The last section shows how to modify the value of an existing column. This script is a good example for beginners learning the fundamentals of DATA STEP and the PRINT procedure.
Data Analysis
Type : CREATION_INTERNE
All data is created and integrated directly into the SAS script using the `DATALINES` statement within each DATA STEP. No external data source is required or used.
1 Code Block
DATA STEP Data
Explanation : This code block creates a SAS table named `Grades1`. The `input` statement defines the data structure by reading variables from specific column positions. Data is provided directly in the code via `DATALINES`.
Copied!
DATA Grades1;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
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;
3
DATALINES;
4
1011 Alia Bhatt 2100658384999196
5
1012 Maria Smith 1788286 . 1009597
6
1111 Thomas Jones 288819669919098
7
1121 Benedictine Arnold 268828289899399
8
1301 Trisha Gupta 1516979598564100
9
;
10
RUN;
2 Code Block
PROC PRINT
Explanation : This procedure displays the entire content of the `Grades1` table in the results output window.
Copied!
PROC PRINT DATA=Grades1;
RUN;
1
PROC PRINTDATA=Grades1;
2
RUN;
3 Code Block
DATA STEP Data
Explanation : Creates a second table `Grades2`, identical to `Grades1`, to demonstrate a different display functionality.
Copied!
DATA Grades2;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
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;
3
DATALINES;
4
1011 Alia Bhatt 2100658384999196
5
1012 Maria Smith 1788286 . 1009597
6
1111 Thomas Jones 288819669919098
7
1121 Benedictine Arnold 268828289899399
8
1301 Trisha Gupta 1516979598564100
9
;
10
RUN;
4 Code Block
PROC PRINT
Explanation : Displays the `Grades2` table. The `var` statement is used to specifically select the columns to display and define their order.
Copied!
PROC PRINT DATA=Grades2;
var name exam1 exam2 exam3 exam4 project finalexam;
RUN;
1
2
PROC PRINT
3
DATA=Grades2;
4
var name exam1 exam2 exam3 exam4 project finalexam;
5
RUN;
6
5 Code Block
DATA STEP Data
Explanation : Creates a third table `Grades3` to illustrate another variation of column display.
Copied!
DATA Grades3;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
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
DATALINES;
4
1011 Alia Bhatt 2100658384999196
5
1012 Maria Smith 1788286 . 1009597
6
1111 Thomas Jones 288819669919098
7
1121 Benedictine Arnold 268828289899399
8
1301 Trisha Gupta 1516979598564100
9
;
10
RUN;
6 Code Block
PROC PRINT
Explanation : Displays the `Grades3` table and uses the `var` statement to reorder columns in a different order than their natural order in the table.
Copied!
PROC PRINT DATA=Grades3;
var name project finalexam exam1 exam2 exam3 exam4;
RUN;
1
2
PROC PRINT
3
DATA=Grades3;
4
var name project finalexam exam1 exam2 exam3 exam4;
5
RUN;
6
7 Code Block
DATA STEP Data
Explanation : Creates the `Grades4` table. A new variable `totalscores` is calculated by summing the scores from exams, project, and final exam. Note that if one of the calculation variables is missing ('.') as for 'Maria Smith', the `totalscores` result will be missing.
Copied!
DATA Grades4;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
totalscores = exam1 + exam2 + exam3 + exam4 + exam5 + project + finalexam;
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 Grades4;
2
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
Explanation : Displays the `Grades4` table, including the newly calculated `totalscores` column to verify the calculation result.
Copied!
PROC PRINT DATA=Grades4;
var name exam1 exam2 exam3 exam4 project finalexam totalscores;
RUN;
1
2
PROC PRINT
3
DATA=Grades4;
4
var name exam1 exam2 exam3 exam4 project finalexam totalscores;
5
RUN;
6
9 Code Block
DATA STEP Data
Explanation : Creates the `Grades5` table. In this DATA STEP, the value of the existing variable `exam2` is modified by adding 1 to it for each observation.
Copied!
DATA Grades5;
input subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
exam2 = exam2 + 1;
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 Grades5;
2
INPUT subj 1-4 name $ 5-23 sex exam1 exam2 exam3 exam4 exam5 project finalexam;
3
exam2 = exam2 + 1;
4
DATALINES;
5
1011 Alia Bhatt 2100658384999196
6
1012 Maria Smith 1788286 . 1009597
7
1111 Thomas Jones 288819669919098
8
1121 Benedictine Arnold 268828289899399
9
1301 Trisha Gupta 1516979598564100
10
;
11
RUN;
10 Code Block
PROC PRINT
Explanation : Displays the `Grades5` table to visualize the result of the modification made to the `exam2` column.
Copied!
PROC PRINT DATA=Grades5;
var name exam1 exam2 exam3 exam4 project finalexam ;
RUN;
1
2
PROC PRINT
3
DATA=Grades5;
4
var name exam1 exam2 exam3 exam4 project finalexam ;
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.
« This script serves as the foundational blueprint for SAS programming. It perfectly illustrates the primary SAS workflow: reading raw data, performing logical transformations in the DATA step, and generating structured reports via PROC PRINT. »
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.