Published on :

Basic SAS DATA and PROC Examples

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins with a series of comments explaining the structure of a SAS© program, the distinction between DATA and PROC steps, as well as syntax rules like the use of semicolons and global declarations. It then proceeds with a practical demonstration:
1. Creation of a new dataset 'myclass' from 'sashelp.class', adding a column for height in centimeters.
2. Displaying the content of 'myclass' with PROC PRINT.
3. Calculating descriptive statistics (mean) for 'age' and 'heightcm' with PROC MEANS.
4. A demonstration of condensed syntax where spacing is not critical, creating and displaying 'myclass1'.
5. Creation of an 'under13' dataset by filtering 'sashelp.class' for individuals under 13 years old.
Data Analysis

Type : MIXED


Source data comes from the SASHELP library (sashelp.class). New datasets (myclass, myclass1, under13) are created and manipulated internally within the script.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a new dataset called 'myclass'. It reads observations from the 'sashelp.class' dataset and adds a new variable 'heightcm' by multiplying the existing 'height' variable by 2. 'run;' terminates the DATA step.
Copied!
1DATA myclass;
2 SET sashelp.class;
3 heightcm=height*2;
4RUN;
2 Code Block
PROC PRINT
Explanation :
This PROC PRINT step displays the contents of the 'myclass' dataset in the results window. It is a common procedure for quickly examining data.
Copied!
1PROC PRINT DATA=myclass;
2RUN;
3 Code Block
PROC MEANS
Explanation :
This PROC MEANS step calculates descriptive statistics (by default: N, mean, standard deviation, min, max) for the 'age' and 'heightcm' variables from the 'myclass' dataset. 'var' specifies the variables for which statistics should be calculated.
Copied!
1PROC MEANS DATA=myclass;
2 var age heightcm;
3RUN;
4 Code Block
DATA STEP / PROC PRINT Data
Explanation :
These two lines demonstrate that SAS is not sensitive to spacing. They create a 'myclass1' dataset identical to 'sashelp.class' and display it immediately. This is a condensed version of the previous blocks, illustrating syntax flexibility.
Copied!
1 
2DATA myclass1;
3SET sashelp.class;
4 
5RUN;
6PROC PRINT
7DATA=myclass1;
8 
9RUN;
10 
5 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates an 'under13' dataset by selecting only observations from 'sashelp.class' where the 'AGE' variable is less than 13. The 'where' clause is used for filtering observations. Lines with asterisks are SAS comments.
Copied!
1DATA under13;
2 SET sashelp.class;
3 where AGE<13;
4 *comment with * should be end with semicolon;
5 *drop heIGht Weight;
6RUN;
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.