Statistical
Software > SAS/WIN Web > Debugging
SAS Program
SAS Online Tutorial
VI. Debugging SAS Program
In the previous section, "What
happens when you make an error", we learned that the log window enables
you to identify and correct errors in your program. The log window contains
three types of messages: Notes, Warnings and Errors. Although Notes and
Warnings will not cause the program to terminate, they are worthy of your
attention, since they may alert you to potential problems. An error message
(usually in red) is more serious, since it indicates that the program
has failed and stopped execution. Many errors are
easily corrected. We next list the most common errors. If your program
fails to run, look for the common errors first.
Common Errors
Missing semicolon
This is by far the most common
error. A missing semicolon will cause SAS to misinterpret not only the
statement where the semicolon is missing, but possibly several statements
that follow. Type or copy/paste the following program into the Editor Window and submit it.
Note that there is no semicolon after data=exercise.
proc freq data=exercise
tables jog tennis;
run;
You will then get the following Log Window:
The missing semicolon causes SAS to read the two statements as a single
statement. As a result, the tables statement is read as an option to the
"proc freq" procedure statement. Since there is no "tables"
option in the frequency procedure statement, you get the Syntax error,
EXPECTING ONE OF THE FOLLOWING: and a list of all the possible options
for the procedure statement.
Always look at the statements
immediately above the line with the error. SAS will underline the error
where it detects it, but sometimes the actual error is in a different
place in your program, typically the preceding line.
Misspellings
Sometimes SAS will correct
your spelling mistakes for you by making its best guess at what you meant
to do. When this happens, SAS will continue execution and issue a warning
explaining the assumption it has made. Type or copy/paste the following
program into your Editor window and submit:
proc freq data=exercise;
tbles jog tennis;
run;
You will then get the following Log Window:
The Log Window has given a warning about the misspelling but corrects
the problem and runs the program. Never assume that a program that has
run without errors is correct. Always review the SAS log for notes and
warnings as well as errors. Type or copy/paste the following program into
the Editor Window and run it. The program runs without errors or warnings
and produces output, but is it correct?
data exercise;
input name $ height weight jog $ tennis $;
wtkg=wieght*2.2;
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;
The Log Window is a follows:
The Log window contains a note that "Variable
wieght is uninitialized. We were attempting
to create a new variable called wtkg which was to be variable weight
multiplied by 2.2, we misspelled weight as "wieght".
Sometimes missing values are legitimate. However, when a variable is missing
for every record in the file (5 at 21:12, means it happened 5 times
at program statement line 21 in column 12, which is the command wtkg=wieght*2.2), there
may be a problem with the program. Indeed if we look at the output window
below, we see we have no values for variable wtkg or wieght. When we referred to the non-existent variable wieght, a new variable called wieght was created, with missing values for all cases.
Missing RUN Statement
Each step in a SAS program
is compiled and executed independently from every other step. As a step
is compiled, SAS recognizes the end of the current step when it finds
a DATA or PROC statement (which indicates the beginning of a new step)
or a RUN statement (which indicates the end of the current step.
When the program below is submitted,
the DATA step executes, but the PROC step does not. The PROC step does
not execute because there is no subsequent DATA or PROC step to indicate
the beginning of a new step, nor is there a RUN statement to indicate
the end of the proc print.
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;
Because there is nothing to indicate the end of the PROC step, the PRINT
procedure waits before running and a "PROC PRINT running" message
appears at the top of the active window. There are no errors in the log, yet you get no output from PROC PRINT.
To correct the problem, submit a RUN statement to complete the PROC step.
run;
You do not need to submit the PROC PRINT again. Indeed, if you do, it will run twice!
Unbalanced Quotes
Unbalanced quotes will result
in a variety of errors because SAS will fail to read subsequent statements
correctly. Your program may appear to be doing nothing, because SAS is
waiting for the end of the quoted string before continuing. Type
or copy/paste the following program which is missing the ending quote
on the Title statement:
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;
title 'unbalanced
quotes;
run;
SAS does not get the "run" statement. It reads it
as part of the title statement, because the title statement is missing
the closing quotes. When submitted, the program would appear to be doing
nothing. System messages would indicate that it is running, as shown below,
which in fact it is. However, SAS is looking for the rest of the program,
waiting for the end of the step, which it can't find because it has
become part of the title statement.
Once you recognize the problem, correcting the unbalanced quotation marks
and resubmitting your program usually does not solve the problem. SAS
still considers the quotes to be unbalanced. To resolve the error, first
submit a quote followed by a semicolon and a RUN statement. The following
would be the only statement in the program editor window:
'; run;
This resolves the missing quote problem. Then you can correct and resubmit the original program.
Home | Back | Next
|