Introduction to SAS


This document describes how to create and run SAS programs under SAS for Windows. Refer to Introduction to SAS for Windows for general information on SAS for Windows. See SAS Program Steps for an overview of SAS procedures and SAS programming statements.

A Simple SAS Program

In the Program Editor window, type in the following lines to create the following file, named EXAMPLE1.SAS.
options ls=65 nodate;
data class;
   input name $ sex $ age height weight;
   datalines;
JOHN      M 12 59.0  99.5
JAMES     M 12 57.3  83.0
ALFRED    M 14 69.0 112.5
WILLIAM   M 15 66.5 112.0
JEFFREY   M 13 62.5  84.0
RONALD    M 15 67.0 133.0
THOMAS    M 11 57.5  85.0
PHILIP    M 16 72.0 150.0
ROBERT    M 12 64.8 128.0
HENRY     M 14 63.5 102.5
JANET     F 15 62.5 112.5
JOYCE     F 11 51.3  50.5
JUDY      F 14 64.3  90.0
CAROL     F 14 62.8 102.5
JANE      F 12 59.8  84.5
LOUISE    F 12 56.3  77.0
BARBARA   F 13 65.3  98.0
MARY      F 15 66.5 112.0
ALICE     F 13 56.5  84.0
proc print;
proc means;
   variables height weight;
When you have entered these lines, save the file:

Submit the program lines

You could then submit the program lines to SAS by pressing the F3 (SUBMIT) key, or click the Submit button in the ToolBar in the main SAS window. The results should appear in the OUTPUT window.

Examine your results

It is advisable to view the LOG first to ensure that the job has run without problem. We do this by selecting the Log window (Click in the Log Window if it is visible, otherwise, choose Log from the Window memu.) The following file is displayed:

NOTE: Copyright (c) 1989-1993 by SAS Institute Inc., Cary, NC, USA.
NOTE: SAS (r) Proprietary Software Release 6.10  TS019
      Licensed to YORK UNIVERSITY, Site 0009300001.

NOTE: The SAS System for Microsoft Windows, Release 6.10 Limited Production
1    options ls=68 nodate;
2    data class;
3       input name $ sex $ age height weight;
4    datalines;

NOTE: The data set WORK.CLASS has 19 observations and 5 variables.
NOTE: The DATA statement used 2.68 seconds.


24   proc print;

NOTE: The PROCEDURE PRINT used 1.32 seconds.


25   proc means;
26      variables height weight;

NOTE: The PROCEDURE MEANS used 0.81 seconds.

To see the results of the SAS procedures we have used, select the Output Window (Click in the Output Window if it is visible, otherwise, choose Output from the Window memu.) It should look like this:

                           The SAS System                          1

          OBS    NAME       SEX    AGE    HEIGHT    WEIGHT

            1    JOHN        M      12     59.0       99.5
            2    JAMES       M      12     57.3       83.0
            3    ALFRED      M      14     69.0      112.5
            4    WILLIAM     M      15     66.5      112.0
            5    JEFFREY     M      13     62.5       84.0
            6    RONALD      M      15     67.0      133.0
            7    THOMAS      M      11     57.5       85.0
            8    PHILIP      M      16     72.0      150.0
            9    ROBERT      M      12     64.8      128.0
           10    HENRY       M      14     63.5      102.5
           11    JANET       F      15     62.5      112.5
           12    JOYCE       F      11     51.3       50.5
           13    JUDY        F      14     64.3       90.0
           14    CAROL       F      14     62.8      102.5
           15    JANE        F      12     59.8       84.5
           16    LOUISE      F      12     56.3       77.0
           17    BARBARA     F      13     65.3       98.0
           18    MARY        F      15     66.5      112.0
           19    ALICE       F      13     56.5       84.0
                           The SAS System                          2

Variable   N          Mean       Std Dev       Minimum       Maximum
--------------------------------------------------------------------
HEIGHT    19    62.3368421     5.1270752    51.3000000    72.0000000
WEIGHT    19   100.0263158    22.7739335    50.5000000   150.0000000
--------------------------------------------------------------------

Printing your Log and Output files

To print a window, select Print from the File menu when a windown is the active (selected) window.

When you tell SAS to print your job the output is routed to a printer automatically assigned to you when you logon. If you want your output printed at a different printer you must select a different printer with the Print Manager application.

External Files

SAS Data Step in a Separate File

For some courses, the data for assignments will be stored on the server in SAS program files, which contains all the statements necessary to create the SAS data set. You could copy these files to your own disk, but there is an easier way.

The file EXAMPLE1.SAS is stored on the server in the directory N:\psy3030\examples. You can tell SAS to include this file in your own program with the statement

     %include 'N:\psy3030\examples\example1.sas';
SAS will then search for EXAMPLE1 SAS, and insert that file exactly as if it were present in your own program.

So, if you wanted to carry out a regression analysis of the CLASS data set, you could make your own file, CLASSREG.SAS, which looked like this:


	%include 'N:\psy3030\examples\example1.sas';


	Proc Reg data=CLASS;
		Model WEIGHT = HEIGHT;
		Id    NAME;
This way, you don't have to clutter up your directory with copies of files that are stored on the server. Moreover, if the version on the server is changed, you always get the current version.

Pre-defined Directory Abbreviations

Some courses (e.g., PSY 3030, PSY 6140, etc) have defined special abbreviations for course-related directories. For example, Psych 3030 has defined a directory abbreviation NWK for the data files from Neter, Wasserman, and Kutner. To include one of these files (say, NWKP0403.SAS), use the form
%include nwk(nwkp0403);

Raw Data in a separate file

There are times when you will wish to access data which is already in a file in raw data form, as when the data you want to analyze has been stored on the server. In this case all you need to do is create a file containing SAS statements and indicate to SAS where the data can be found. Suppose, for example, that you have a file containing raw data residing on your own diskette which is called: DATA.RAW

Let's say it looks like this:

JOHN      M 12 59.0  99.5
JAMES     M 12 57.3  83.0
ALFRED    M 14 69.0 112.5
WILLIAM   M 15 66.5 112.0
JEFFREY   M 13 62.5  84.0
RONALD    M 15 67.0 133.0
THOMAS    M 11 57.5  85.0
PHILIP    M 16 72.0 150.0
ROBERT    M 12 64.8 128.0
 ... (more data lines)
ALICE     F 13 56.5  84.0

We can use SAS to analyse or manipulate this data by creating a different file containing only SAS statements. We create a new file called EXAMPLE2 SAS

To start a new program, clear the Program Editor window, as follows: Select the Program Editor window, then choose Clear text from the Edit menu. Then type in the following statements:

filename indata 'a:data.raw';
data class;
   infile indata;
   input name $ sex $ age height weight;
proc print;
proc means;
   var height weight;

Notice the difference between this SAS program and our previous example. A FILENAME statement has been added at the beginning of the program to inform the SAS supervisor where to find our data. We indicate that our data is in a file named DATA.RAW on the A: drive, and that we are going to refer to that file as INDATA. Whenever we use the name INDATA in our program the SAS supervisor will now know that we are referring to our data file: A:DATA.RAW.

The FILENAME statement associates a logical name, INDATA, with the physical name, A:DATA.RAW, of a file. Generally, any file can be specified in this manner, and any logical name can be used to refer to it in the program (eg. we could have used the name EXCITING instead of INDATA). However note that 8 letters is the maximum length for a label in SAS.

The second alteration to the previous example is the INFILE INDATA; statement which is used to tell the SAS supervisor that the data must be read from an external raw data file. Note - it replaces the CARDS; statement in our original example and is placed before the INPUT statement.

Once your file has been set up in this manner you "run" or execute it in the same manner as our first example.

SAS System Files

Creating SAS System Files

It is sometimes useful to use SAS system files to organize and store your data compactly. In SAS for Windows, creating and accessing SAS system files is a relatively easy task. Using as an example the first SAS program we created, we merely give the DATA statement a "two-level" name. The original DATA CLASS statement becomes DATA SAVE.CLASS.
EXAMPLE3 SAS A

libname save 'a:';
data save.class;
 input name $ sex $ age height weight;
 datalines;
JOHN      M 12 59.0  99.5
JAMES     M 12 57.3  83.0
ALFRED    M 14 69.0 112.5
WILLIAM   M 15 66.5 112.0
JEFFREY   M 13 62.5  84.0
RONALD    M 15 67.0 133.0
THOMAS    M 11 57.5  85.0
PHILIP    M 16 72.0 150.0
 ... (more data lines)
ALICE     F 13 56.5  84.0
proc print;
proc means ;
 var height weight;

When this job is submitted to SAS, a file CLASS.SD2 is created in the directory (A:\ specified in the LIBNAME statement. This is a SAS system file and contains the data created by the data step starting DATA SAVE.CLASS.

Note that the LIBNAME statement associates a physical directory with the first part of the dataset name (SAVE).

Using SAS System Files

You can use SAS system files to create new data files or you can invoke them directly in a PROC statement without need of employing a DATA step. In the first example below a SAS system file is employed to create a new data file containing data for males only to be used in a plotting procedure.

EXAMPLE4 SAS
libname save 'a:';
data males;
   set save.class;
   if sex='M';
proc print;
proc plot; plot weight*height=sex;

The following output appears in the Output Window. (The output from the PROC PLOT step is not shown.)

        OBS    NAME       SEX    AGE    HEIGHT    WEIGHT

          1    JOHN        M     12      59.0       99.5
          2    JAMES       M     12      57.3       83.0
          3    ALFRED      M     14      69.0      112.5
          4    WILLIAM     M     15      66.5      112.0
          5    JEFFREY     M     13      62.5       84.0
          6    RONALD      M     15      67.0      133.0
          7    THOMAS      M     11      57.5       85.0
          8    PHILIP      M     16      72.0      150.0
          9    ROBERT      M     12      64.8      128.0
         10    HENRY       M     14      63.5      102.5

If there is no need to alter the data in the file, statistical analyses can be performed by calling the data directly from the PROC statement. Below is an example:

libname save 'a:';
proc print data=save.class;
proc means data=save.class;
  var height weight:

This performs our original analysis (EXAMPLE1); there is no need for a DATA step since the data file has already been created by SAS in an earlier job.

SAS Documentation

  1. SAS Procedures Guide, Version 6 Edition
  2. SAS/STAT User's Guide, Version 6 Edition -- all the statistical procedures are described here.
  3. SAS Companion for the Microsoft Windows Environment -- all details about SAS specific to Windows.
  4. SAS/GRAPH User's Guide, Version 6 Edition