Skip to Main Content

An overview of the syntax of SAS procedures | SAS Learning Modules

1. Introduction

This module will illustrate the general syntax of SAS
procedures. We will use the auto data file, shown below, to illustrate the syntax of SAS
procedures.

DATA auto ;
  input MAKE $ PRICE MPG REP78 FOREIGN ;
DATALINES;
AMC    4099 22 3 0
AMC    4749 17 3 0
AMC    3799 22 3 0
Audi   9690 17 5 1
Audi   6295 23 3 1
BMW    9735 25 4 1
Buick  4816 20 3 0
Buick  7827 15 4 0
Buick  5788 18 3 0
Buick  4453 26 3 0
Buick  5189 20 3 0
Buick 10372 16 3 0
Buick  4082 19 3 0
Cad.  11385 14 3 0
Cad.  14500 14 2 0
Cad.  15906 21 3 0
Chev.  3299 29 3 0
Chev.  5705 16 4 0
Chev.  4504 22 3 0
Chev.  5104 22 2 0
Chev.  3667 24 2 0
Chev.  3955 19 3 0
Datsun 6229 23 4 1
Datsun 4589 35 5 1
Datsun 5079 24 4 1
Datsun 8129 21 4 1
;
RUN;

2. Using a procedure with no options

Now, lets have a look at the use of SAS procedures using proc
means
as an example.  Here we show that it is possible to use proc means with no options at all.  By default,SAS uses the last data file created
(i.e., auto) and it provides means for all of the numeric variables in the
data file.

PROC MEANS ;
RUN; 

Here you see the results of the means procedure from the auto data file.
SAS displays the sample size (N), mean, standard deviation , minimum and maximum for all of the numeric variables.

Variable     N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------------------
PRICE       26         6651.73         3371.12         3299.00        15906.00
MPG         26      20.9230769       4.7575042      14.0000000      35.0000000
REP78       26       3.2692308       0.7775702       2.0000000       5.0000000
FOREIGN     26       0.2692308       0.4523443               0       1.0000000
------------------------------------------------------------------------------

3. Using options on the PROC statement

We can use the data= option to tell proc means
to tell SAS what data file will be used to perform the means
procedure. The data= option comes right after proc means.  Even though the
data= option is optional, we strongly recommend
using it every time because it avoids errors of omission when you revise your programs.

PROC MEANS DATA=auto;
RUN; 

As you see, the results are identical to those above.

Variable     N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------------------
PRICE       26         6651.73         3371.12         3299.00        15906.00
MPG         26      20.9230769       4.7575042      14.0000000      35.0000000
REP78       26       3.2692308       0.7775702       2.0000000       5.0000000
FOREIGN     26       0.2692308       0.4523443               0       1.0000000
------------------------------------------------------------------------------

We can use the n, mean and std options to
tell proc means that we just want the N, mean and standard
deviation for the data. 

PROC MEANS DATA=auto N MEAN STD ;
RUN; 

The output, shown below, shows just the N, mean, and
standard deviation, just as we requested.

Variable     N            Mean         Std Dev
----------------------------------------------
PRICE       26         6651.73         3371.12
MPG         26      20.9230769       4.7575042
REP78       26       3.2692308       0.7775702
FOREIGN     26       0.2692308       0.4523443
----------------------------------------------

These examples have shown us that you can have options
on the proc statement, for example after proc means we
used the data= , n, mean and std options.

4. Using additional statements

Proc means also supports additional statements.  Here we use the var statement to say which variable(s)
we want SAS to produce means for.

PROC MEANS DATA=auto;
  VAR price ;
RUN;

As you would expect, the output shows the results just
for the variable price.

Analysis Variable : PRICE
 N            Mean         Std Dev         Minimum         Maximum
------------------------------------------------------------------
26         6651.73         3371.12         3299.00        15906.00
------------------------------------------------------------------

Here we also use the class statement to
request means broken down by  the variable foreign (i.e., foreign and domestic cars).

PROC MEANS DATA=auto;
  CLASS foreign ;
  VAR price ;
RUN; 

As we requested, the means of price are
shown for the two levels of foreign.

Analysis Variable : PRICE
               N
     FOREIGN  Obs   N          Mean       Std Dev       Minimum       Maximum
-----------------------------------------------------------------------------
           0   19  19       6484.16       3768.46       3299.00      15906.00
            1    7   7       7106.57       2101.83       4589.00       9735.00
-----------------------------------------------------------------------------

These examples have shown that you can have additional
statements with a proc (for example, the var and
class
statements). Each proc has its own set of additional statements that are valid
for that proc.

5. Options on additional statements

It is also possible to have options on the additional
statements lines (the statements after the proc statement).  We will illustrate this
using proc reg.

Here we use proc reg to predict price
from mpg.  We use the model statement to tell
proc
reg
that we want to predict price from mpg.

PROC REG DATA=auto ;
  MODEL price = mpg ;
RUN;
QUIT; 

Here is the output from the proc reg.

Model: MODEL1  
Dependent Variable: PRICE                                              

Analysis of Variance

                         Sum of         Mean
Source          DF      Squares       Square      F Value       Prob>F

Model            1 54620027.581 54620027.581        5.712       0.0251
Error           24 229491191.53 9562132.9806
C Total         25 284111219.12

    Root MSE    3092.26988     R-square       0.1922
    Dep Mean    6651.73077     Adj R-sq       0.1586
    C.V.          46.48820

Parameter Estimates

                 Parameter      Standard    T for H0:               
Variable  DF      Estimate         Error   Parameter=0    Prob > |T|

INTERCEP   1         13152  2786.6930753         4.720        0.0001
MPG        1   -310.689641  129.99546608        -2.390        0.0251

Notice that we don’t get standardized estimates
(betas).  We have to ask proc reg to give those to us.  In
particular, we use the stb option on the model
statement, as shown below.  Note that the stb option comes after a
forward slash ( / ).  Options on a proc statement come right after the name of the proc,
but options for subsequent statements must follow a forward slash.

PROC REG DATA=auto ;
  MODEL price = mpg / STB;
RUN; 

The output is the same as the output above, except that
it also includes the portion shown below that has the standardized estimates (betas).

              Standardized
Variable  DF      Estimate

INTERCEP   1    0.00000000
MPG        1   -0.43846180

6. More examples

We have illustrated the general syntax of SAS procedures
using proc means and proc reg.  Let’s look at a few more examples, this
time using proc freq.  As you may imagine, proc freq is used for
generating frequency tables.  From what we have learned, we would expect that
proc freq
would have:

– Options on the proc freq statement that would influence
the way that the tables look.
– Additional statements that would specify what tables to
produce.
– Options on the additional statements lines that would influence
how those particular tables look.

Let’s examine some examples.  

First, consider the program below.  As you might
expect, the program below would generate frequency tables for every variable in the auto
data file.

PROC FREQ DATA=auto;
RUN; 

If we use the page option, proc freq will
start every table on a new page.  Note that this influences all of the tables
produced in that proc freq procedure.

PROC FREQ DATA=auto PAGE;
RUN; 

We have also seen that a SAS procedure can have one or
more optional statements.  Below we show that we can have one or more
tables
statements to specify the frequency tables we want, in this case, tables for
rep78
and price.  Because we used the page option, each table will start on a
new page.  This influences both the tables made for rep78
and price. 
(Note that we could have specified tables rep78 price, and gotten the same result,
but we wanted to illustrate having more than one tables statement.)

PROC FREQ DATA=auto PAGE;
  TABLES rep78 ;
  TABLES price ;
RUN; 

As we might expect, we could supply options on each of
the tables statements to determine how those particular tables are
to be shown.  The
example below requests frequency tables for rep78 and price, but the table
for rep78 will omit percentages because it used the
nopercen
t option. 
Both tables will appear on a new page (because the page option influences all of
the tables) but only rep78 will suppress the printing of percentages because
the nopercent option only applies to that one tables statement.

PROC FREQ DATA=auto PAGE;
  TABLES rep78 / NOPERCENT ;
  TABLES price ;
RUN; 

7. Problems to look out for

When you use options, it is easy to confuse an option
that goes on the proc statement with options that follow on subsequent statements.

8. For more information