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

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 PROCS
  1. The first procedure, PROC MEANS, calculates the mean for every variable.
  2. 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
  1. 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.
  2. The variables (ALF, UTL, ASL, etc.) are read in by specifying the columns where the data are stored.
  3. 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
  4. adds the NTYPE values to the data set.
  5. The program labels the output so that the display will be easier to understand,
  6. 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
  1. The program then generates frequency counts for the levels of TYPE and NTYPE,
  2. Computes the mean of the variables VAR, ALF, UTL, ASL, SPA, and CPM by NTYPE, and
  3. 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
  1. 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.
  2. Performs a regression procedure on ALF, UTL, ASL, and SPA.
  3. Program requests optional output (predicted values, 95% confidence limits for mean, sequential and partial sums of squares, and standardized regression coefficients).
  4. 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.
  5. Outputs the residuals to a temporary SAS data set called WORK.D in a variable called RCPM.
  6. 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.


Return to Top Document