Вы находитесь на странице: 1из 6

SAS-stuf Page 1

Tutorial 1: SAS Programs

Program Steps

Introduction
DATA Step
PROC Step
SAS Statements
A Simple SAS Program
Separate Data Files

Introduction

Version 8 of SAS provides for both interactive and command language input. In these tutorials
we will focus on the command interface. SAS programs are made up of SAS statements
which are similar to English sentences. It is important to remember that each statement or
sentence ends with a semicolon. The two most important types of SAS statements are the
DATA and the PROC statements. In SAS, DATA and PROC statements are used to define
two basic program steps.

The, DATA steps create or modify SAS datasets and PROC steps tell SAS what analyses are
to be conducted on the dataset. Some programs will not have a PROC step, but almost all will
have a DATA step.

A DATA or PROC section continues until all of the commands in that section are completed.
The end of a section is indicated when another DATA or PROC statement appears or when
SAS encounters a RUN statement.

DATA Step

The DATA step creates a SAS dataset that contains the data along with a "data dictionary."
The data dictionary contains information on the variables and their properties (whether they
are numeric or character, the width of the values at input, etc.)

The following example creates a SAS data set from raw input:
DATA EXAMPLE1;
INPUT NAME $ SEX $ AGE INCOME;
CARDS;
Susan F 18 12000

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM


SAS-stuf Page 2

Fred M 20 21586
Jane F 19 22232
(many observations omitted)
John M 19 14128
;

Notice that each command line ends with a semicolon. Also, the dollar sign after the variables
NAME and SEX indicate that those variables are character variables and not numbers. It is
also good practice to put a semicolon at the end of the data set. This is not essential but it does
provide a logical break point.

The above DATA step inputs the data but does nothing with it. To conduct an analysis, we
need a PROC statement.

PROC Step

The PROCedure step is used to perform some type of analysis on the data, including
PRINTing it. The following are examples of PROC statements.

PROC PRINT;
PROC MEANS;
VARIABLES AGE INCOME;
RUN;

According to the SAS documentation, the RUN command is optional in some versions of
SAS. Our version of SAS, however, does seem to require it. Together, the DATA and the
PROC steps make up a SAS program.

SAS statements
SAS statements follow certain rules so that the program can understand what you want.
Specifically,

All SAS statements start with a keyword (e.g., DATA, PROC)


All SAS statements end with a semicolon (;). The semicolon is like a period at the end of
a sentence written in English.
SAS statements can start anywhere on a line. However, readability is enhanced by using
indents in the same way we indent paragraphs in written English. Furthermore, spaces
and blank lines are ignored so it doesn't matter if you put more than one space between
words.

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM


SAS-stuf Page 3

SAS does not distinguish between upper and lower case letters. Consequently, "PROC"
is the same as "Proc." Upper and lower case does matter with data, however, since the
statements SEX= 'F' and SEX= 'f' are not equivalent.
missing data are represented by a . (a period or dot).

A Simple SAS Program


Step 1: Copy the following program into the SAS dialogue window.
TITLE 'My first SAS run';
OPTIONS ls=65;
DATA grades;
INPUT name $ midterm final;
CARDS;
Susan 60 65
Bill 70 72
Monica 85 81
Francine 78 78
Jessica 72 74
Orin 80 82
;
PROC PRINT data=grades;
RUN;

Once these lines have been copied into the dialogue window, you should save them in a file
for future use. To do this, you choose Save As from the File menu select the directory where
you want to save the file (for example, a floppy disk on drive A:) give the file a name such as
EXAMPLE1.SAS

Step 2: Submit the program lines to SAS.

This is done by either pressing the F3 key or by clicking the Submit button (the running
figure) on the ToolBar

Step 3: Look at your results in the OUTPUT window.

f you have not made a typing error, the OUTPUT window should contain the following:

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM


SAS-stuf Page 4

Step 4 (optional): You can keep your output for future use by either sending it to a printer or
by saving it as a file. To print the contents of a window, make sure that you have clicked on
the window (making it the active one, or the one in the forefront). Select Print from the File
menu. This will send the file to the printer. Selecting Save As allows you to save the output as
a file

The DATA step allows you to read data that is stored in an external file or to create a SAS data
file. External files are convenient in that you do not have to re-type your information every
time you want to use SAS. SAS data files save the data in binary as opposed to text format.
They are especially useful when you have a very large data set (say, over 1,000 cases) since
they speed up the data input or reading process. The DATA step also allows you to conduct
"transformations" on the data by using different arithmetic and logical operators.

Separate Data Files


Data are often kept in a separate file to avoid having to re-enter them every time we want to
use SAS. External data sets are also easy to share with other researchers. These files must be in
text or ASCII format. To ensure this, create them with NotePad or another text editor, or save

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM


SAS-stuf Page 5

them using the "*.txt" option in WordPerfect or MS Word after selecting Save As on the File
menu.

Let's assume that you have entered a data file into NotePad (or some similar text editor) and
saved it into a separate file called: Example.raw

The file might look like this:


Susan F 18 12000
Fred M 20 21586
Jane F 19 22232
Wendy F 22 25000
Bill M 24 25589
John M 19 14128

Get into the SAS Program Editor (select Clear from the Edit menu if there is already
something there). Type the following statements:

FILENAME MYDATA 'A:EXAMPLE.RAW';


DATA CLASS;
INFILE MYDATA;
INPUT NAME $ SEX$ AGE INCOME;
PROC PRINT;
PROC MEANS;
VARIABLES AGE INCOME;
RUN;

Make sure you have a semicolon at the end of each statement!

The first line in this program assigns a logical filename (in this case 'MYDATA') to the name
of your physical file ('A:EXAMPLE.RAW'). From now on, SAS will refer to your data file by
its logical name. Think of this logical name as an alias or nickname. Just as nicknames are
usually a short form for something much longer, so logical filenames can avoid your having to
type out something like 'c:/sas/students/data/classnote/example1.raw' more than once.

The second line ('DATA CLASS;') tells SAS you are invoking the DATA step and that you
want to give your data the temporary name 'CLASS.' SAS does not like to work with the
original data set more than it has to so it creates temporary data sets internally. This minimizes
the possibility of altering (i.e., 'screwing-up') the original data.

The third line ('INFILE MYDATA') tells SAS to get, or input, your data file under the logical
filename MYDATA and copy it into the temporary file called CLASS. This line replaces the
CARDS line that we used in the first tutorial.

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM


SAS-stuf Page 6

The fourth line (the INPUT line) tells SAS the names of your variables just as it did in the first
example.

The two remaining, or PROC lines, indicate what you want done to the data. If the PROC
statement is not included in the same run, you may have to use the DATA= option to tell SAS
which data set you are using. For example: PROC PRINT DATA=CLASS;

Revised: August 25, 2000

http://instruct.uwo.ca/sociology/300a/SAStut1.htm 12/11/2007 12:05:48 PM

Вам также может понравиться