Statistical
Software > SAS/WIN Web > Overview of
SAS
SAS Online Tutorial
IV. Overview of a SAS Program
With SAS, you use statements
to write a series of instructions called a SAS program. The program communicates
what you want to do and is written using the SAS language. There are menu
driven front ends to SAS,(SAS/ASSIST and the Analyst, for example), which
make SAS appear like a point-and-click program. However, these front ends
still use the SAS language to write programs for you. You will have much
more flexibility using SAS if you learn to write your own programs using
the SAS language.
Components of a SAS Program
SAS programs consist of two
basic building blocks: DATA steps and PROC steps. A typical SAS program
starts with a DATA step to create or manipulate a SAS data set and then
passes the data to PROC step for processing.
SAS language syntax
SAS programs are made up of
SAS statements. A SAS statement begins with a keyword and always ends
with a semicolon. A DATA step begins with the keyword DATA. A PROC step
begins with the keyword PROC. SAS statements are free-format, so they
can begin and end anywhere on a line, one statement can continue over
several lines, and several statements can be on a line. Blanks or special
characters separate "words" in a SAS statement.
In the program below (used
in the previous section), we have a DATA step to create a SAS dataset
called exercise which contains 5 variables and 5 observations. This is followed by a PROC step to print the newly created SAS dataset, exercise.
data exercise;
input name $ height weight jog $ tennis $;
datalines;
Robert 68 150 yes yes
George 67 180 no no
Agatha 63 110 no no
Sandy 60 125 yes yes
Bill 65 160 yes no
proc print data=exercise;run;
All the keywords in the above example are in blue.
We begin with a DATA step (keyword data) to create a temporary SAS dataset
called exercise. The DATA step begins with the keyword data and ends with
the line containing the keyword proc. The only information necessary on
the data statement in this example is the keyword data and then a name
for the SAS dataset we are about to create (exercise). The semicolon ends
the data statement.
The next statement begins with
the keyword input. It defines the variables to be read from each line of
data. We have 5 names following the keyword input, corresponding to the 5 pieces
of information on each data record.
You will notice that some of the names are followed by a dollar sign ($).
This tells SAS that the information for that data (i.e. variables:
name, jog, tennis) is not numeric but character data. Without specific
instructions SAS assumes all data is numeric.
The DATALINES statement
tells SAS that the next lines contain the data. Notice that the lines
of data do not end in a semicolon. Once SAS reads the DATALINES statement,
SAS assumes all subsequent lines are data until it finds a semicolon, or either the keywords proc or run.
The proc print ends the DATA step and tells SAS to process the new
dataset (exercise) by listing it in the output window. The final run statement
tells SAS to process the previous statements. Your final statement in
a SAS program must always run.
Rules for SAS names
You have already been creating
SAS dataset names (e.g. exercise) and SAS variable names (e.g. name, height,
weight, jog, tennis) without knowing the rules. So to avoid future problems
here our the rules:
- Names must be 32 characters
or fewer in length
- Names must start with a
letter or an underscore(_)
- Names can contain only letters,
numerals, or underscores(_).No %,$,etc.
- Names can contain upper
and lowercase letters but SAS will not distinguish them (i.e. Height is the same as HEIGHT or height)
Saving a SAS Program
Now that we have a SAS program
with several lines, we will save the program for future use. If we do
not do this, when we exit the SAS System, this program will be lost. Activate
the Editor window by clicking in it, then from the FILE pull-down menu
select Save as. When the Save as dialog box appears, enter a name for
the SAS program (e.g. example) and select the drive and directory where you wish to save the
SAS program (e.g. a:\). Check that the file type is *.sas. Click
on OK. This saves this program in a:\ with the name example.sas.
Exiting the SAS system
Now that we have saved the program,
we can exit the SAS system and come back later and pick up where
we left off. To exit from SAS, from the FILE menu click on Exit.
Using a Saved SAS program
To retrieve the prviously saved program, start SAS.
From the FILE menu, select Open Program (or Open, depending on your version of SAS). When the Open dialog box appears, click
on the directory where you saved the program (e.g. a:\), then select example.sas,
then click on Open. Once the example.sas program appears in the Editor
window, then you can Submit (click on the running figure) to get back
to where you were before you exited SAS.
Home | Back | Next
|