Overview
This document provides three different examples of SAS programs and
explains the different characteristics of each program.
A link to each sample program, with a brief description of its
characteristics, is provided below. Additional links to the program text, the
accompanying explanation, and any additional information, are provided in the
Overview section of each sample program.
- Sample Program #1 : Reads data from an
inline source and uses two PROCs.
- Sample Program #2 : Reads data from an
external
file, creating a permanent SAS data set, and uses five PROCs.
- Sample Program #3 : Uses a permanent data
set in a PROC, generating a temporary data set, and using the temporary
data set in a subsequent PROC.
Sample Program #1
Overview
Following is the first of three examples of SAS programs.
The program is written out first, and an explanation of the program
is provided in the subsequent section.
Program
DATA CLASS;
INPUT NAME $ 1-8 SEX $ 10 AGE 12-13 HEIGHT 15-16 WEIGHT 18-22;
CARDS;
JOHN M 12 59 99.5
JAMES M 12 57 83.0
ALFRED M 14 69 112.5
ALICE F 13 56 84.0
;
PROC MEANS;
VAR AGE HEIGHT WEIGHT;
PROC PLOT;
PLOT WEIGHT*HEIGHT;
ENDSAS;
Explanation
The Data Step
- The DATA statement tells the computer that the data is coming from
an inline source, and that it will need to create a temporary data file
called CLASS. Temporary SAS data sets have one-part names as shown
above. Permanent SAS data sets are described in Sample Program #2.
- The INPUT statement formats the variables for the computer.
- NAME: this is an alphanumeric variable, as indicated by
the $. The variable NAME has been assigned columns 1-8.
- SEX: this is also an alphanumeric variable, and has been
assigned column 10.
- AGE: this is a numeric variable, and has been assigned columns 12-13.
- HEIGHT: numeric variable, columns 15-16.
- WEIGHT: numeric variable, columns 18-22.
- The CARDS statement informs the computer that the data is located in
the next lines. Data lines must not end in semicolons and the first line
after the end of the data must consist of a semicolon on a line by itself,
a PROC statement, or a DATA step.
The PROCS
- The first procedure, PROC MEANS, calculates the mean for every variable.
- The second, PROC PLOT, plots the values for WEIGHT against HEIGHT.
Sample Program #2
Overview
Following is the second of three examples of SAS programs.
The program is written out first, and an explanation of the program
is provided in the subsequent section.
Sample Program
DATA "~/data/airdata";
INFILE 'air.dat' ;
INPUT ALF 1-5 ULT 7-11 ASL 13-17 SPA 19-24 TYPE $ 26 CMP 28-32;
IF TYPE = 'A' THEN NTYPE=1; ELSE NTYPE=0
LABEL ALF='PERCENT OF SEATS OCCUPIED'
UTL='AVERAGE HOURS/DAY USE OF AIRCRAFT'
ASL='AVERAGE LENGTH OF NONSTOP LEGS'
SPA='AVERAGE NUMBER OF SEATS PER AIRCRAFT'
CPM='COST PER PASSENGER MILE'
TYPE='SHORT VS. LONG NONSTOP LEGS'
NTYPE='NUMERIC TYPE VARIABLE';
PROC SORT; BY NTYPE;
PROC PRINT; BY NTYPE;
PROC FREQ;
TABLES TYPE NTYPE;
PROC MEANS;
VAR ALF UTL ASL SPA CPM; BY NTYPE;
PROC CORR;
VAR ALF UTL ASL SPA CPM;
ENDSAS;
Explanation
The Data Step
- The program reads data from an external file called air.dat
and creates a permanent SAS data set in the directory "~/data". This
data set is called airdata.sas7bdat. The extension is
automatic and should not be specified by the user. Note that this is a
change from earlier versions of SAS which required a libname statement
and a two-part filename using a libref.
- The variables (ALF, UTL, ASL, etc.) are read in by specifying the columns where the data are stored.
- The program then codes the numeric variable called NTYPE with a 1 if TYPE is an A, or with a 0 if TYPE is anything else except A, and
- adds the NTYPE values to the data set.
- The program labels the output so that the display will be easier to understand,
- Sorts the data in ascending order, with records for which NTYPE = 0 appearing first in the file. The data will be read out in this sorted order.
The PROCS
- The program then generates frequency counts for the levels of TYPE and NTYPE,
- Computes the mean of the variables VAR, ALF, UTL, ASL, SPA, and CPM by NTYPE, and
- Intercorrelates these variables.
Sample Program #3
Overview
This is the third of three examples of SAS programs.
The program is written out first, and an explanation of the program
is provided in the subsequent section.
Program
PROC REG DATA = "~/data/airdata";
MODEL CPM=ALF UTL ASL SPA / P CLM SS1 SS2 STB;
TEST1 : TEST SPA,ALF;
TEST2 : TEST UTL-ALF=0;
OUTPUT OUT=D R=RCPM;
PROC PLOT DATA=D;
PLOT RCPM*CPM;
ENDSAS;
Explanation
The PROCS
- This program accesses the data set created in Example 2. No
extension of sas7bdat should be given for the permanent SAS data file
since it is assumed by SAS.
- Performs a regression procedure on ALF, UTL, ASL, and SPA.
- Program requests optional output (predicted values, 95% confidence limits for mean, sequential and partial sums of squares, and standardized regression coefficients).
- Tests the null hypotheses that the regression weights for SPA and ALF are 0, and that the regression weights for UTL and ALF are equal.
- Outputs the residuals to a temporary SAS data set called WORK.D in a variable called RCPM.
- Makes a graph plotting RCPM against the observed values of CPM, with RCPM values assigned to the Y-axis and CPM values assigned to the X-axis.