About HPC Systems Software User Guides Education Partners

  / gears / hpc / education / tutorials / sas / fieldguide


Overview

Starting Out with SAS

Analyzing and Work with Data

Recommended Books

SAS Field Guide

SAS Facts

Formatting and Commenting SAS Programs

SAS Field Guide


When you work with SAS, you write a SAS program or "SAS code". Your SAS code is a series of instructions, written in the "SAS language". These instructions typically start-out with some brief specifications on output constraints (for example, page size, line size, whether you want the output centered or left-justified, etc). These specifications are usually followed by some definitions detailing where to find data files, what format the data files are written in, what to call the files, and how to input the data. After this, normally one would write instructions to read the data into a SAS dataset (a form that SAS can use while it's running). As the data are read into a SAS dataset, variables optionally may be recoded (modified) or other variables can be calculated and added into the dataset. Any of a number of "data manipulations", including very complicated restructuring can be done as data are input into a SAS dataset. After the instructions to create a SAS dataset come instructions to tell SAS which statistical procedures to perform and with what specifications.

The two steps of creating a SAS dataset and running statistical procedures are divided in SAS as DATA steps and PROCedures. Data steps begin with a DATA statement and end with a run; statement or, if there is no run; statement, then it ends at the next DATA step or PROC. These DATA and PROCedure steps can be interspersed throughout a SAS program to accomplish several different analyses on any of a number of different data files or SAS datasets. But it is very important to realize that you cannot combine a PROCedure within a DATA step, and you cannot perform data manipulations within a PROCedure. Higher level functions (eg. output) of some SAS PROCedures allow one to create new datasets from the results of the PROCedure, but aside from such special options, DATA and PROC steps must be written as distinct operations in your SAS code.

Most programming in SAS is simple and nearly understandable as "structured English". SAS will handle most of the details of looping through the observations of the data, so you don't need to write "do loops" unless you intend to do some data restructuring or iterative recoding. The statistical analyses that SAS can perform work somewhat like "function calls" or "procedures" in other programming languages--the complicated mathematics of an analysis are already written for you as part of the PROCedure--you merely need to "call" the PROCedure and specify which dataset to use and with what parameters.

When you're done writing your SAS code, you submit the code, or set of instructions, to SAS. SAS will read and interpret what you wrote and perform the tasks you specified.

Although it is possible to include your data as part of your SAS code, the usual method is to keep your data in a separate file and write instructions in your SAS code for reading the data from an external data file. A SAS session may be composed of several components (see diagram below). You write your SAS code as one file and your data reside in a file of their own. You submit your SAS code for processing to "The SAS System", a series of programs, interpreters, and libraries that make up "SAS". The SAS system interprets your code and carries-out the tasks you specified, including reading-in any data files you may have specified in your SAS code. As SAS finishes processing your code, the results are written to an OUTPUT window (or directly to a listing [*.lst] file, if running batch) and a transcript of the SAS session, including any error messages, warnings, and notes are written to a LOG window (or *.log file, if running batch).

   +---------------------------+   +---------------------------+
   |                           |   |                           |
   |   SAS Program or "Code"   |   |   Data file on disk or    |
   |      that you write       |   |   tape or CD-ROM, etc     |
   |                           |   |                           |
   |                           |   |                           |
   +---------------------------+   +---------------------------+
                          |             |
                          V             V
                   +---------------------------+    +-------------------+
                   |                           |    |                   |
                   |                           |    |      LOG file     |
                   |      The SAS System       |--->|  Includes errors, |
                   |                           |    |  warnings, notes. |
                   |                           |    |                   |
                   +---------------------------+    +-------------------+
                                 |
                                 V
                   +---------------------------+
                   |                           |
                   |                           |
                   |      Output (Results)     |
                   |                           |
                   |                           |
                   +---------------------------+

Normally, you will work with three windows: The Program Editor, The LOG, and The OUTPUT windows. In the Program Editor window you create, open, or modify a SAS program. LOG window contains the execution log of a session, and the output of SAS programs are in the OUTPUT window.

  • Program Editor: Your SAS code goes here.
  • LOG: A log of your SAS session. This is where notes, warnings, and errors are reported.
  • OUTPUT: This is where your results appear.

When you are ready to execute a SAS program that you prepared in the Program Editor window, SAVE it to your floppy disk first, then from the LOCAL pull-down menu choose SUBMIT. Alternatively, you can click on the submit button, which looks like a little stick figure running. As the program is executed, a transcript of the session--with any error messages--will be written to the LOG window. If the program runs correctly, the OUTPUT window will display the results. Always review the LOG window after running your program--even if it appears that your program ran correctly. Some serious errors, such as misread or incorrectly aligned data, may go unnoticed otherwise. Always check for error messages and warnings in the LOG window.

The Program Editor window will become empty after you submit a job. If you forgot to save it, you can choose RECALL TEXT from the LOCAL pull-down menu (or press F4).

When you save your work, you should follow these conventions:

  1. The name of a SAS source code should have extension .sas;
  2. The log of a session should have extension .log;
  3. The output should have extension .lst.

SAS is portable, which means your SAS applications function the same, look the same, and produce the same results on any hardware platform and under any operating system. You can develop SAS applications in one environment and run them in other environments without rewriting any code except for a few lines where you may be specifying external data files.

Sample SAS Program

Here's a sample SAS program, modified slightly from a suggestion by Haihong Li of the Penn State Department of Statistics. The parts in blue are actual SAS commands, or program steps. The parts written in black are "comments", or notes, to ourselves (ie. humans) about the program. SAS ignores--skips over--any comments within a program and works only on actual program steps:

OPTIONS PS=66 LS=72;

/***********************************************************************
* The above OPTIONS statement is a SAS system option, it sets the
* page size to be 66 lines, and 72 characters per line.
***********************************************************************/

/***********************************************************************
* Filename: fitness.sas
* Written by: Haihong Li
* Date: June 10, 1996

* A small program to demonstrate the basic structure of a SAS program
* that contains data as part of the program itself.
***********************************************************************/

/* Create a dataset called "fitness": */

DATA fitness;
  INPUT name $ weight waist pulse chins situps jumps;
  CARDS;
  Hodges    191  36  50   5  162   60
  Kerr      189  37  52   2  110   60
  Putnam    193  38  58  12  101  101
  Roberts   162  35  62  12  105   37
  Blake     189  35  46  13  155   58
  ;
RUN;

/* Sort the dataset "fitness" by name: */

PROC SORT;
  BY name;
RUN;

/* Print out the sorted dataset: */

PROC PRINT data=fitness;
  title 'Fitness data';
RUN;

/* Get some descriptive statistics: */

PROC MEANS maxdec=1 data=fitness;
RUN;



If you run it, you'll get the output like this:


                                 Fitness data   21:24 Monday, June 10, 1996   1
    OBS    NAME       WEIGHT    WAIST    PULSE    CHINS    SITUPS    JUMPS

     1     BLAKE        189       35       46       13       155       58 
     2     HODGES       191       36       50        5       162       60 
     3     KERR         189       37       52        2       110       60 
     4     PUTNAM       193       38       58       12       101      101 
     5     ROBERTS      162       35       62       12       105       37 


                                 Fitness data   21:24 Monday, June 10, 1996   2
     Variable  N          Mean       Std Dev       Minimum       Maximum
     -------------------------------------------------------------------
     WEIGHT    5         184.8          12.9         162.0         193.0
     WAIST     5          36.2           1.3          35.0          38.0
     PULSE     5          53.6           6.4          46.0          62.0
     CHINS     5           8.8           5.0           2.0          13.0
     SITUPS    5         126.6          29.4         101.0         162.0
     JUMPS     5          63.2          23.3          37.0         101.0
     -------------------------------------------------------------------

Please send questions or suggestions about this web page to beatnic@aset.psu.edu

ASET | ITS | Penn State