Overview 68
Introduction 68
Objectives 68
Line Numbers 76
Example 77
Example 78
Example 81
Interpreting Error Messages 81
Error Types 81
Example 82
Chapter Quiz 95
Now that you're familiar with the basics, you can learn how to use the SAS programming windows to edit and debug programs effectively.
So far, you've copied and pasted SAS programs into the code editing window, and you have submitted them for execution. You can also open a stored program in the code editing window, where you can edit the program and submit it again.
You can include a program using
file shortcuts
My Favorite Folders
the Open window
the INCLUDE command.
File shortcuts are available in the SAS Explorer window. To include a program using file shortcuts:
At the top level of the SAS Explorer window, double-click File Shortcuts
.
Double-click the file shortcut that you want to include, or select Open
from the pop-up menu for the file.
The file opens in a new code editing window, with the file name shown as the window title.
? beside the item to display pop-up menus
S beside the item to simulate a double-click.
To view and manage any files in your operating environment, you can use the My Favorite Folders window.
To include a file that is stored in My Favorite Folders:
Select View
Double-click the file, or select Open
from the pop-up menu for the file.
The file opens in a new code editing window, with the file name shown as the window title.
To use the Open window:
With the code editing window active, select File
In the Open window, click the file that you want to include (or type the path for the file).
Click Open
or OK
.
The file opens in a new code editing window, with the file name shown as the window title.
If you're not sure of the name of the file in which your program is stored, you can do either of the following:
use My Favorite Folders to locate the file in your operating environment
issue the X
command to temporarily suspend your SAS session.
The X command enables you to use operating system facilities without ending your SAS session. When you issue the X command, you temporarily exit SAS and enter the host system.
To resume your SAS session after issuing the X command, issue the appropriate host system command, as shown below.
Operating Environment | Host command to resume SAS session |
---|---|
CMS | RETURN |
OS/2 | EXIT |
z/OS | RETURN or END |
UNIX | exit |
VMS | LOGOFF |
Windows | EXIT |
Now that you know how to open a SAS program, let's review the characteristics of SAS statements and look at enhancing the readability of your SAS programs.
Remember that SAS programs consist of SAS statements. Take a look at the SAS program that is shown in a code editing window below.
Although you can write SAS statements in almost any format, a consistent layout enhances readability and helps you understand the program's purpose. It's a good idea to
begin DATA and PROC steps in column one
indent statements within a step
begin RUN statements in column one
include a RUN statement after every DATA step or PROC step.
data work.bankacct; infile records;
input Name $ 1-10 AccountType $ 12-20 Deposit 22-25 Withdrawal 27-30; run; proc print data=work.bankacct; run; proc means mean; var deposit withdrawal; run;
Now let's look at the Enhanced Editor window features that you can use to edit your SAS programs.
When you edit SAS programs in the Enhanced Editor, you can take advantage of a number of useful features. The following section shows you how to
use color-coding to identify code elements
automatically indent the next line when you press the Enter key
collapse and expand sections of SAS procedures, DATA steps, and macros
bookmark lines of code for easy access to different sections of your program
open multiple views of a file.
You can also
save color-coding settings in a color scheme
create macros that record and play back program editing commands
create shortcuts for typing in text using abbreviations
customize keyboard shortcuts for most Enhanced Editor commands
create and format your own keywords
automatically reload modified disk files
access Help for SAS procedures by placing the insertion point within the procedure name and pressing F1.
The Enhanced Editor uses visual aides such as color coding and code sections to help you write and debug your SAS programs.
You can use the margin on the left side of the Editor window to
select one or more lines of text
expand and collapse code sections
display bookmarks.
To find text in the Editor window, select Edit
You can define a character string so that when you type it and then press the Tab key or the Enter key, the string expands to a longer character string.
For example, you could define the abbreviation myv7sasfiles, which would expand to 'c:winntprofilesmyidpersonalmysasfilesv7';
. Abbreviations are actually macros that insert one or more lines of text.
To create an abbreviation,
Select Tools
For Abbreviation
, type the name of the abbreviation.
For Text to insert for abbreviation
, type the text that the abbreviation will expand into.
Click OK
.
To use an abbreviation, type the abbreviation. When an abbreviation is recognized, a tooltip displays the expanded text. Press the Tab key or Enter key to accept the abbreviation.
You can modify or delete abbreviations that you create.
When you use the Enhanced Editor, you can see different parts of the same file simultaneously by opening multiple views of the same file. While you are working with multiple views, you are working with only one file, not multiple copies of the same file.
To open multiple views of the same file,
make the file the active window.
select Window
The filename in the title bar is appended with a colon and a view number. For example, in the illustration above, the file names are vansales.sas:1 and vansales.sas:2.
Changes that you make to a file in one view, such as changing text or bookmarking a line, occur in all views simultaneously. Actions such as scroll bar movement, text selection, and expanding or contracting a section of code occur only in the active window.
You can customize the Enhanced Editor by setting Enhanced Editor options. To open the Enhanced Editor Options window, activate an Editor window and select Tools
Click the tabs that are located along the top of the window to navigate to the settings that you want to change, and then select the options that you want.
For example, Show line numbers
specifies whether to display line numbers in the margin. When line numbers are displayed, the current line number is red. Insert spaces for tabs
specifies whether to insert the space character or the tab character when you press the Tab key. If it is selected, the space character is used. If it is not selected, the tab character is used.
When you are finished, click OK
.
You use the Program Editor window to submit SAS programs. You can also enter and edit SAS programs in this window, or you can open existing SAS programs that you created in another text editor.
To edit SAS programs in the Program Editor window, you can display line numbers. Then you can use text editor line commands and block text editor line commands within the line numbers to insert, delete, move, and copy lines within the Program Editor window.
In some operating environments, line numbers appear in the Program Editor window by default. In other systems, the use of line numbers is optional. Activating line numbers can make it easier for you to edit your program regardless of your operating environment.
To turn on line numbers, use the NUMS command. Type nums on the command line or in the command box and press Enter. To turn line numbers off, enter the NUMS command again. Line numbers activated using the NUMS command will remain in effect for the duration of your SAS session.
Text editor line commands enable you to delete, insert, move, copy, and repeat text. You enter these commands in the line number area in the Program Editor window.
The table below summarizes the basic text editing line commands.
Command | Action |
---|---|
Cn | copies n lines (where n = a number up to 9999) |
Dn | deletes n lines |
In | inserts n blank lines |
Mn | moves n lines |
Rn | repeats current line n times |
A | after (used with C, I, and M) |
B | before (used with C, I, and M) |
You can use text editor line commands to perform actions like these:
Command | Action |
---|---|
00001 | inserts 3 lines after line 00002 |
i3 002 | |
00003 | |
0ib 01 | inserts 1 line before line 00001 |
00002 | |
00003 | |
0ib 41 | inserts 4 lines before line 00001 |
00002 | |
00003 | |
000c2 | copies 2 lines (00001 and 00002) after line 00003 |
00002 | |
0a 003 | |
00001 | deletes 3 lines (00002, 00003, and 00004) |
0d3 02 | |
00003 | |
00b 01 | moves 1 line (00003) before line 00001 |
00002 | |
00m 03 |
In the example below, a PROC PRINT statement and a RUN statement need to be inserted after line 00004.
To insert the PROC PRINT and RUN statements in the program,
Type i2
anywhere in the line number area for line 00004 in the Program Editor window. Two blank lines are inserted, and the cursor is positioned on the first new line.
Type the PROC PRINT statement on line 00005.
Type a RUN statement on line 00006.
Block text editor line commands enable you to delete, repeat, copy, and move multiple lines in the Program Editor window.
The block text editor line commands include the following:
Command | Action |
---|---|
DD | deletes a block of lines |
CC | copies a block of lines |
MM | moves a block of lines |
RR | repeats multiple lines |
A | after (used with CC and MM commands) |
B | before (used with CC and MM commands) |
To use a block command, specify the command on the first line affected and on the final line affected, and then press Enter.
In the program below, the PROC TABULATE step needs to be deleted.
You can use the DD block command to remove the step. To use the DD command:
Type DD
anywhere in the line number area for line 00007 to mark the first line affected.
Type DD
anywhere in the line number area for line 00010 to mark the final line affected.
Press Enter to delete lines 00007-00010.
In the previous practice, SAS statements disappeared from the Program Editor window when you submitted them. However, you can recall a program to the Program Editor by selecting Run
The area in which submitted SAS code is stored is known as the recall buffer. Program statements accumulate in the recall buffer each time you submit a program. The recall buffer is last-in/first-out (the most recently submitted statements are recalled first).
For example, if you submit two programs, you will need to select Run
To save your SAS program to an external file, first activate the code editing window and select File
You can also save a SAS program by issuing a FILE command.
By this time, you may have noticed that text accumulates in the Output window and in the Log window.
At this point, you may find it helpful to clear the contents of your SAS programming windows. To clear the Output window, Editor window, Program Editor window, or Log window, activate each window individually and select Edit
So far, the programs that you've submitted in this chapter have been error-free, but programming errors do occur. SAS can detect several types of errors. The most common are
syntax errors that occur when program statements do not conform to the rules of the SAS language
data errors that occur when some data values are not appropriate for the SAS statements that are specified in a program.
This chapter focuses on identifying and correcting syntax errors.
When you submit a program, SAS scans each step for syntax errors, then processes the step (if no syntax errors are found). SAS then goes to the next step and repeats the process. Syntax errors, such as misspelled words, generally cause SAS to stop processing the step in which the error occurred.
You already know that information is written to the Log window while a SAS program is executing. When a program that contains an error is submitted, messages regarding the problem also appear in the Log window. When a syntax error is detected, the Log window
displays the word ERROR
identifies the possible location of the error
gives an explanation of the error.
The program below contains a syntax error. The DATA step copies the SAS data set Clinic.Admit into a new data set named Clinic.Admitfee. The PROC step should print the values for the variables ID, Name, Actlevel, and Fee in the new data set. However, print is misspelled in the PROC PRINT statement.
data clinic.admitfee;
set clinic.admit;
run;
proc prin data=clinic.admitfee;
var id name actlevel fee;
run;
When the program is submitted, messages in the Log window indicate that the procedure PRIN was not found and that SAS stopped processing the PRINT step due to errors. No output is produced by the PRINT procedure, because the second step fails to execute.
To modify programs that contain errors, you can edit them in the code editing window. You can correct simple errors, such as the spelling error in the following program, by typing over the incorrect text, deleting text, or inserting text.
data clinic.admitfee;
set clinic.admit;
run;
proc prin data=clinic.admitfee;
var id name actlevel fee;
run;
In the program below, the missing t has been inserted into the PRINT keyword that is specified in the PROC PRINT statement.
After correcting your program, you can submit it again. However, before doing so, it's a good idea to clear the messages from the Log window so that you don't confuse the old error messages with the new messages. Then you can resubmit the program and view any resulting output.
Remember to check the Log window again to verify that your program ran correctly.
In addition to correcting spelling mistakes, you might need to resolve several other types of common syntax errors. These errors include
omitting semicolons
leaving quotation marks unbalanced
specifying invalid options.
Another common problem is omitting a RUN statement at the end of a program. Although this is not technically an error, it can produce unexpected results. For the sake of convenience, we'll consider it together with syntax errors.
The table below lists these problems and their symptoms.
Problem | Symptom |
---|---|
missing RUN statement | "PROC (or DATA) step running" at top of active window |
missing semicolon | log message indicating an error in a statement that seems to be valid |
unbalanced quotation marks | log message indicating that a quoted string has become too long or that a statement is ambiguous |
invalid option | log message indicating that an option is invalid or not recognized |
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 encounters
a DATA or PROC statement, which indicates the beginning of a new step
a RUN or QUIT 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 following DATA or PROC step to indicate the beginning of a new step, nor is there a following RUN statement to indicate the end of the step.
data clinic.admitfee; set clinic.admit; run; proc print data=clinic.admitfee; var id name actlevel fee;
Because there is nothing to indicate the end of the PROC step, the PRINT procedure waits before executing, and a "PROC PRINT running" message appears at the top of the active window.
To correct the error, submit a RUN statement to complete the PROC step.
run;
If you are using the Program Editor window, you do not need to recall the program to the Program Editor window.
One of the most common errors is the omission of a semicolon at the end of a statement. The program below is missing a semicolon at the end of the PROC PRINT statement.
data clinic.admitfee;
set clinic.admit;
run;
proc print data=clinic.admitfee
var id name actlevel fee;
run;
When you omit a semicolon, SAS reads the statement that lacks the semicolon, plus the following statement, as one long statement. The SAS log then lists errors that relate to the combined statement, not the actual mistake (the missing semicolon).
To correct the error, do the following:
Find the statement that lacks a semicolon. You can usually locate the statement that lacks the semicolon by looking at the underscored keywords in the error message and working backwards.
Add a semicolon in the appropriate location.
Resubmit the corrected program.
Check the Log window again to make sure there are no other errors.
Some syntax errors, such as the missing quotation mark after HIGH in the program below, cause SAS to misinterpret the statements in your program.
data clinic.admitfee;
set clinic.admit;
where actlevel='HIGH;
run;
proc print data=clinic.admitfee;
var id name actlevel fee;
run;
When the program is submitted, SAS is unable to resolve the DATA step, and a "DATA STEP running" message appears at the top of the active window.
When unbalanced quotation marks appear in a program that contains TITLE or FOOTNOTE statements, there is sometimes a warning in the SAS log which indicates that
a quoted string has become too long
a statement that contains quotation marks (such as a TITLE or FOOTNOTE statement) is ambiguous due to invalid options or unquoted text.
When you have unbalanced quotation marks, SAS is often unable to detect the end of the statement in which it occurs. Simply adding a quotation mark and resubmitting your program usually does not solve the problem. SAS still considers the quotation marks to be unbalanced.
Therefore, you need to resolve the unbalanced quotation mark by canceling the submitted statements (in the Windows and UNIX operating environments) or by submitting a line of SAS code (in the z/OS operating environment) before you recall, correct, and resubmit the program.
To resolve the error in the Windows operating environment:
Press the Ctrl and Break keys or click the the Break Icon
Select 1. Cancel Submitted Statements
, then click OK
.
Select Y to cancel submitted statements
, then click OK
.
Correct the error and resubmit the program.
To resolve the error in the UNIX operating environment:
Open the Session Management window and click Interrupt
.
Select 1. Cancel Submitted Statements
, then click Y
.
Correct the error and resubmit the program.
To resolve the error in the z/OS operating environment:
Submit an asterisk followed by a quotation mark, a semicolon, and a RUN statement.
*'; run;
Delete the line that contains the asterisk followed by the quotation mark, the semicolon, and the RUN statement.
Insert the missing quotation mark in the appropriate place.
Submit the corrected program.
An invalid option error occurs when you specify an option that is not valid in a particular statement. In the program below, the KEYLABEL option is not valid when used with the PROC PRINT statement.
data clinic.admitfee;
set clinic.admit;
run;
proc print data=clinic.admitfee keylabel;
label actlevel='Activity Level';
run;
When a SAS statement that contains an invalid option is submitted, a message appears in the Log window indicating that the option is not valid or not recognized.
You can insert comments into a SAS program to document the purpose of the program, to explain segments of the program, or to describe the steps in a complex program or calculation. A comment statement begins and ends with a comment symbol.
There are two forms of comment statements:
*text;
or
/*text*/
SAS ignores text between comment symbols during processing.
The following program shows some of the ways comments can be used to describe a SAS program.
/* Read national sales data for vans */ /* from an external raw data file */ data perm.vansales; infile vandata; input @1 Region $9. @13 Quarter 1. /* Values are 1, 2, 3, or 4 */ @16 TotalSales comma11.; /* Print the entire data set */ proc print data=perm.vansales; run;
SAS includes several system options that enable you to control error handling and Log window messages. The table shown below contains brief descriptions of some of these options. You can use either the OPTIONS statement or the SAS System Options window to specify these options.
Option | Descriptions |
---|---|
ERRORS=n | Specifies the maximum number of observations for which complete data error messages are printed. |
FMTERR | NOFMTERR | Controls whether SAS generates an error message when a format of a variable cannot be found. NOFMTERR results in a warning instead of an error. FMTERR is the default. |
SOURCE | NOSOURCE | Controls whether SAS writes source statements to the SAS log. SOURCE is the default. |
If you are using the Enhanced Editor, you can use its color-coding for program elements, quoted strings, and comments to help you find coding errors.
You can also search for
ending brackets or parentheses by pressing Ctrl+].
matching DO-END pairs by pressing Alt+[. (You can learn about DO-END pairs in Chapter 15, "Generating Data with DO Loops," on page 473.)
See the following table for suggestions about finding syntax errors using the Enhanced Editor.
To find… | Do this… |
---|---|
Undefined or misspelled keywords | In the Appearance tab of the Enhanced Editor Options dialog box, set the file elements When SAS recognizes a keyword, the keyword changes to the defined colors. You'll be able to easily spot undefined keywords by looking for the colors that you selected for undefined keywords. |
Unmatched quoted strings | Look for one or more lines of the program that are the same color. Text following a quotation mark remains the same color until the string is closed with a matching quotation mark. |
Unmatched comments | Look for one or more lines of the program that are the same color. Text that follows an open comment symbol ( /* ) remains the same color until the comment is closed with a closing comment symbol ( */). |
Matching DO-END pairs | Place the cursor within a DO-END block and press Alt+[. The cursor moves first to the DO keyword. If one of the keywords is not found, the cursor remains as positioned. When both of the keywords exist, pressing Alt+[ moves the cursor between the DO-END keywords. |
Matching parentheses or brackets | Place the cursor on either side of the parenthesis or bracket. Press Ctrl+]. The cursor moves to the matching parentheses or bracket. If one is not found, the cursor remains as positioned. |
Missing semi-colons ( ; ) | Look for keywords that appear in normal text. |
Debugging is the process of removing logic errors from a program. Unlike most syntax errors, logic errors do not stop a program from running. Instead, they cause the program to produce unexpected results.
You can debug logic errors in DATA steps by using the DATA step debugger. This tool allows you to issue commands to execute DATA step statements one by one, and then to pause to display the resulting variables' values in a window. By observing the results that are displayed, you can determine where the logic error lies.
The debugger can be used only in interactive mode. Because the debugger is interactive, you can repeat the process of issuing commands and observing results as many times as needed in a single debugging session. To invoke the debugger, add the DEBUG option to the DATA statement, and execute the program.
data perm.publish / debug;
infile pubdata;
input BookID $ Publisher & $22. Year;
run;
proc print data=perm.publish;
run;
A SAS program that is stored in an external file can be included (copied) into the code editing window using
file shortcuts
My Favorite Folders
the Open window
the INCLUDE command.
SAS programs consist of SAS statements. Although you can write SAS statements in almost any format, a consistent layout enhances readability and enables you to understand the program's purpose.
In the Windows operating environment, the Enhanced Editor enables you to enter and view text and
select one or more lines of text
use color-coding to identify code elements
automatically indent the next line when you press the Enter key
collapse and expand sections of SAS procedures, DATA steps, and macros
bookmark lines of code for easy access to different sections of your program.
Using the Editor window, you can also find and replace text, use abbreviations, open multiple views of a file, and set Enhanced Editor options.
The Program Editor window enables you to edit your programs just as you would with a word processing program. You can also use text editor commands and block text editor commands to edit SAS programs. Activating line numbers can make it easier for you to edit your program regardless of your operating environment.
Remember that SAS statements disappear from the Program Editor window when they are submitted. However, you can recall a program to the Program Editor window.
To save your SAS program to an external file, first activate the Program Editor window and select File
Text and output accumulate in the Editor, Program Editor, Log, and Output windows throughout your SAS session. You can clear a window by selecting Edit
When a SAS program that contains errors is submitted, error messages appear in the Log window. SAS can detect five types of errors: syntax, semantic, execution-time, data and macro-related. This chapter focuses on identifying and resolving common syntax errors.
To modify programs that contain errors, you can correct the errors in the code editing window. In the Program Editor window, you need to recall the submitted statements before editing them.
Before resubmitting a revised program, it's a good idea to clear the messages from the Log window so that you don't confuse old messages with the new. You can delete any error-free steps from a revised program before resubmitting it.
It's a good idea to begin DATA steps, PROC steps, and RUN statements on the left and to indent statements within a step.
End each step with a RUN statement.
Review the messages in the Log window each time you submit a SAS program.
You can delete any error-free steps from a revised program before resubmitting it, or you can submit only the revised steps in a program.
Select the best answer for each question. After completing the quiz, check your answers using the answer key in the appendix.
As you write and edit SAS programs it's a good idea to
begin DATA and PROC steps in column one.
indent statements within a step.
begin RUN statements in column one.
do all of the above.
Suppose you have submitted a SAS program that contains spelling errors. Which set of steps should you perform, in the order shown, to revise and resubmit the program?
Correct the errors.
Clear the Log window.
Resubmit the program.
Check the Log window.
Correct the errors.
Resubmit the program.
Check the Output window.
Check the Log window.
Correct the errors.
Clear the Log window.
Resubmit the program.
Check the Output window.
Correct the errors.
Clear the Output window.
Resubmit the program.
Check the Output window.
What happens if you submit the following program?
proc sort data=clinic.stress out=maxrates; by maxhr; run; proc print data=maxrates label double noobs; label rechr='Recovery Heart Rate; var resthr maxhr rechr date; where toler='I' and resthr>90; sum fee; run;
Log messages indicate that the program ran successfully.
A "PROC SORT running" message appears at the top of the active window, and a log message may indicate an error in a statement that seems to be valid.
A log message indicates that an option is not valid or not recognized.
A "PROC PRINT running" message appears at the top of the active window, and a log message may indicate that a quoted string has become too long or that the statement is ambiguous.
What generally happens when a syntax error is detected?
SAS continues processing the step.
SAS continues to process the step, and the Log window displays messages about the error.
SAS stops processing the step in which the error occurred, and the Log window displays messages about the error.
SAS stops processing the step in which the error occurred, and the Output window displays messages about the error.
A syntax error occurs when
some data values are not appropriate for the SAS statements that are specified in a program.
the form of the elements in a SAS statement is correct, but the elements are not valid for that usage.
program statements do not conform to the rules of the SAS language.
none of the above
How can you tell whether you have specified an invalid option in a SAS program?
A log message indicates an error in a statement that seems to be valid.
A log message indicates that an option is not valid or not recognized.
The message "PROC running" or "DATA step running" appears at the top of the active window.
You can't tell until you view the output from the program.
Which of the following programs contains a syntax error?
proc sort data=sasuser.mysales; by region; run;
dat sasuser.mysales; set mydata.sales99; run;
proc print data=sasuser.mysales label; label region='Sales Region'; run;
none of the above
What should you do after submitting the following program in the Windows or UNIX operating environment?
proc print data=mysales; where state='NC; run;
Submit a RUN statement to complete the PROC step.
Recall the program. Then add a quotation mark and resubmit the corrected program.
Cancel the submitted statements. Then recall the program, add a quotation mark, and resubmit the corrected program.
Recall the program. Then replace the invalid option and resubmit the corrected program.
Which of the following commands opens a file in the code editing window?
file 'd:programssas ewprog.sas'
include 'd:programssas ewprog.sas'
open 'd:programssas ewprog.sas'
all of the above
Suppose you submit a short, simple DATA step. If the active window displays the message "DATA step running" for a long time, what probably happened?
You misspelled a keyword.
You forgot to end the DATA step with a RUN statement.
You specified an invalid data set option.
Some data values weren't appropriate for the SAS statements that you specified.