Chapter 3. Editing and Debugging SAS Programs

Overview

Introduction

Now that you're familiar with the basics, you can learn how to use the SAS programming windows to edit and debug programs effectively.

Introduction

Objectives

In this chapter you learn to

  • include a stored SAS program

  • edit SAS programs

  • clear SAS programming windows

  • interpret error messages in the SAS log

  • correct errors

  • resolve common problems.

Opening a Stored SAS Program

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.

Opening a Stored SAS Program
Opening a Stored SAS Program

Using File Shortcuts

File shortcuts are available in the SAS Explorer window. To include a program using file shortcuts:

  1. At the top level of the SAS Explorer window, double-click File Shortcuts.

  2. Double-click the file shortcut that you want to include, or select Open from the pop-up menu for the file.

    Using File Shortcuts

The file opens in a new code editing window, with the file name shown as the window title.

Using File Shortcuts
Using File Shortcuts
  • ? beside the item to display pop-up menus

  • S beside the item to simulate a double-click.

Using My Favorite Folders

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:

  1. Select View

    Using My Favorite Folders
    My Favorite Folders.

  2. Double-click the file, or select Open from the pop-up menu for the file.

Using My Favorite Folders

The file opens in a new code editing window, with the file name shown as the window title.

Using My Favorite Folders

Using the Open Window

To use the Open window:

  1. With the code editing window active, select File

    Using the Open Window
    Open (or File
    Using the Open Window
    Open Program
    ).

  2. In the Open window, click the file that you want to include (or type the path for the file).

  3. Click Open or OK.

Using the Open Window

The file opens in a new code editing window, with the file name shown as the window title.

Using the Open Window
Using the Open Window

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 EnvironmentHost command to resume SAS session
CMSRETURN
OS/2EXIT
z/OSRETURN or END
UNIXexit
VMSLOGOFF
WindowsEXIT

Issuing an INCLUDE Command

You can also include a program by issuing an INCLUDE command.

Example

Suppose you want to include the program D:ProgramsSasMyprog1.sas in the Windows operating environment. To do so, you can issue the following INCLUDE command:

include 'd:programssasmyprog1.sas'

Because this is a command (not a SAS statement), no semicolon is used at the end.

Editing SAS Programs

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.

SAS Program Structure

Remember that SAS programs consist of SAS statements. Take a look at the SAS program that is shown in a code editing window below.

SAS Program Structure

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.

SAS Program Structure

Using the Enhanced Editor

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.

Using the Enhanced Editor

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.

Entering and Viewing Text

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.

Finding and Replacing Text

To find text in the Editor window, select Edit

Finding and Replacing Text
Find or press Ctrl+F. You can set commonly used find options, including specifying that the text string is a regular expression. You can also specify whether to search in the code only, in the comments only, or in both the code and comments.

Finding and Replacing Text

Using Abbreviations

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,

  1. Select Tools

    Using Abbreviations
    Add Abbreviation.

  2. For Abbreviation, type the name of the abbreviation.

  3. For Text to insert for abbreviation, type the text that the abbreviation will expand into.

  4. 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.

Opening Multiple Views of a File

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,

  1. make the file the active window.

  2. select Window

    Opening Multiple Views of a File
    New 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.

Setting Enhanced Editor Options

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

Setting Enhanced Editor Options
Options
Setting Enhanced Editor Options
Enhanced Editor
.

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.

Setting Enhanced Editor Options
Setting Enhanced Editor Options

Program Editor Features

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.

Program Editor Features

Line Numbers

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.

Line Numbers

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.

Line Numbers

Text Editor Line Commands

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.

CommandAction
Cncopies n lines (where n = a number up to 9999)
Dndeletes n lines
Ininserts n blank lines
Mnmoves n lines
Rnrepeats current line n times
Aafter (used with C, I, and M)
Bbefore (used with C, I, and M)

You can use text editor line commands to perform actions like these:

CommandAction
00001inserts 3 lines after line 00002
i3002 
00003 
0ib01inserts 1 line before line 00001
00002 
00003 
0ib41inserts 4 lines before line 00001
00002 
00003 
000c2copies 2 lines (00001 and 00002) after line 00003
00002 
0a003 
00001deletes 3 lines (00002, 00003, and 00004)
0d302 
00003 
00b01moves 1 line (00003) before line 00001
00002 
00m03 

Example

In the example below, a PROC PRINT statement and a RUN statement need to be inserted after line 00004.

Example

To insert the PROC PRINT and RUN statements in the program,

  1. 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.

  2. Type the PROC PRINT statement on line 00005.

  3. Type a RUN statement on line 00006.

Example

Block Text Editor Line Commands

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:

CommandAction
DDdeletes a block of lines
CCcopies a block of lines
MMmoves a block of lines
RRrepeats multiple lines
Aafter (used with CC and MM commands)
Bbefore (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.

Example

In the program below, the PROC TABULATE step needs to be deleted.

Example

You can use the DD block command to remove the step. To use the DD command:

  1. Type DD anywhere in the line number area for line 00007 to mark the first line affected.

  2. Type DD anywhere in the line number area for line 00010 to mark the final line affected.

  3. Press Enter to delete lines 00007-00010.

Example

Recalling SAS Programs

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

Recalling SAS Programs
Recall Last Submit.

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

Recalling SAS Programs
Recall Last Submit two times to recall the first program to the Program Editor window. You can recall statements any time during your SAS session.

Recalling SAS Programs

Saving SAS Programs

To save your SAS program to an external file, first activate the code editing window and select File

Saving SAS Programs
Save As, then specify the name of your program in the Save As window. It's good practice to assign the extension .SAS to your SAS programs to distinguish them from other types of SAS files (such as .LOG for log files and .DAT for raw data files).

Saving SAS Programs
Saving SAS Programs

You can also save a SAS program by issuing a FILE command.

Example

Suppose you want to save a program as D:ProgramsSasNewprog.Sas in the Windows operating environment. To do so, you can issue the following FILE command:

file 'd:programssas
ewprog.sas'

Clearing SAS Programming Windows

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

Clearing SAS Programming Windows
Clear All.

Clearing SAS Programming Windows
Clearing SAS Programming Windows
Clearing SAS Programming Windows

Interpreting Error Messages

Error Types

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.

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.

Example

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.

Example
Example

Correcting Errors

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;
Correcting Errors

In the program below, the missing t has been inserted into the PRINT keyword that is specified in the PROC PRINT statement.

Correcting Errors
Correcting Errors

Resubmitting a Revised Program

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.

Resubmitting a Revised Program

Remember to check the Log window again to verify that your program ran correctly.

Resubmitting a Revised Program
Resubmitting a Revised Program
Resubmitting a Revised Program
Resubmitting a Revised Program

Resolving Common Problems

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.

ProblemSymptom
missing RUN statement"PROC (or DATA) step running" at top of active window
missing semicolonlog message indicating an error in a statement that seems to be valid
unbalanced quotation markslog message indicating that a quoted string has become too long or that a statement is ambiguous
invalid optionlog message indicating that an option is invalid or not recognized

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 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.

Missing RUN Statement

Resolving the Problem

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.

Missing Semicolon

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).

Missing Semicolon

Resolving the Problem

To correct the error, do the following:

  1. 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.

  2. Add a semicolon in the appropriate location.

  3. Resubmit the corrected program.

  4. Check the Log window again to make sure there are no other errors.

Unbalanced Quotation Marks

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.

Unbalanced Quotation Marks

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.

Unbalanced Quotation Marks

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.

Unbalanced Quotation Marks

Resolving the Error in the Windows Operating Environment

To resolve the error in the Windows operating environment:

  1. Press the Ctrl and Break keys or click the the Break Icon

    Resolving the Error in the Windows Operating Environment
  2. Select 1. Cancel Submitted Statements, then click OK.

    Resolving the Error in the Windows Operating Environment
  3. Select Y to cancel submitted statements, then click OK.

    Resolving the Error in the Windows Operating Environment
  4. Correct the error and resubmit the program.

Resolving the Error in the UNIX Operating Environment

To resolve the error in the UNIX operating environment:

  1. Open the Session Management window and click Interrupt.

    Resolving the Error in the UNIX Operating Environment
  2. Select 1. Cancel Submitted Statements, then click Y.

    Resolving the Error in the UNIX Operating Environment
  3. Correct the error and resubmit the program.

Resolving the Error in the z/OS Operating Environment

To resolve the error in the z/OS operating environment:

  1. Submit an asterisk followed by a quotation mark, a semicolon, and a RUN statement.

    *'; run;
  2. Delete the line that contains the asterisk followed by the quotation mark, the semicolon, and the RUN statement.

  3. Insert the missing quotation mark in the appropriate place.

  4. Submit the corrected program.

Resolving the Error in the z/OS Operating Environment

Invalid Option

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.

Invalid Option

Resolving the Problem

To correct the error:

  1. Remove or replace the invalid option, and check your statement syntax as needed.

  2. Resubmit the corrected program.

  3. Check the Log window again to make sure there are no other errors.

Additional Features

Comments in SAS Programs

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;
Comments in SAS Programs

System Options

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.

OptionDescriptions
ERRORS=nSpecifies the maximum number of observations for which complete data error messages are printed.
FMTERR | NOFMTERRControls 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 | NOSOURCEControls whether SAS writes source statements to the SAS log. SOURCE is the default.

Error Checking in the Enhanced Editor

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 Defined keyword, User defined keyword, and the Undefined keyword to unique color combinations.

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 with the DATA Step Debugger

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;
Debugging with the DATA Step Debugger
Debugging with the DATA Step Debugger

Chapter Summary

Text Summary

Opening a Stored SAS Program

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.

Editing SAS Programs

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

Editing SAS Programs
Save As, then specify the name of your program in the Save As window.

Clearing SAS Programming Windows

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

Clearing SAS Programming Windows
Clear All.

Interpreting Error Messages

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.

Correcting 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.

Resolving Common Problems

You may need to resolve several types of common problems: missing RUN statements, missing semicolons, unbalanced quotation marks, and invalid options.

Points to Remember

  • 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.

Chapter Quiz

Select the best answer for each question. After completing the quiz, check your answers using the answer key in the appendix.

  1. As you write and edit SAS programs it's a good idea to

    1. begin DATA and PROC steps in column one.

    2. indent statements within a step.

    3. begin RUN statements in column one.

    4. do all of the above.

  2. 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?

    1.  

      1. Correct the errors.

      2. Clear the Log window.

      3. Resubmit the program.

      4. Check the Log window.

    2.  

      1. Correct the errors.

      2. Resubmit the program.

      3. Check the Output window.

      4. Check the Log window.

    3.  

      1. Correct the errors.

      2. Clear the Log window.

      3. Resubmit the program.

      4. Check the Output window.

    4.  

      1. Correct the errors.

      2. Clear the Output window.

      3. Resubmit the program.

      4. Check the Output window.

  3. 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;
    1. Log messages indicate that the program ran successfully.

    2. 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.

    3. A log message indicates that an option is not valid or not recognized.

    4. 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.

  4. What generally happens when a syntax error is detected?

    1. SAS continues processing the step.

    2. SAS continues to process the step, and the Log window displays messages about the error.

    3. SAS stops processing the step in which the error occurred, and the Log window displays messages about the error.

    4. SAS stops processing the step in which the error occurred, and the Output window displays messages about the error.

  5. A syntax error occurs when

    1. some data values are not appropriate for the SAS statements that are specified in a program.

    2. the form of the elements in a SAS statement is correct, but the elements are not valid for that usage.

    3. program statements do not conform to the rules of the SAS language.

    4. none of the above

  6. How can you tell whether you have specified an invalid option in a SAS program?

    1. A log message indicates an error in a statement that seems to be valid.

    2. A log message indicates that an option is not valid or not recognized.

    3. The message "PROC running" or "DATA step running" appears at the top of the active window.

    4. You can't tell until you view the output from the program.

  7. Which of the following programs contains a syntax error?

    1. proc sort data=sasuser.mysales;
         by region;
      run;
    2. dat sasuser.mysales;
         set mydata.sales99;
      run;
    3. proc print data=sasuser.mysales label;
         label region='Sales Region';
      run;
    4. none of the above

  8. What should you do after submitting the following program in the Windows or UNIX operating environment?

    proc print data=mysales;
       where state='NC;
    run;
    1. Submit a RUN statement to complete the PROC step.

    2. Recall the program. Then add a quotation mark and resubmit the corrected program.

    3. Cancel the submitted statements. Then recall the program, add a quotation mark, and resubmit the corrected program.

    4. Recall the program. Then replace the invalid option and resubmit the corrected program.

  9. Which of the following commands opens a file in the code editing window?

    1. file 'd:programssas
      ewprog.sas'
    2. include 'd:programssas
      ewprog.sas'
    3. open 'd:programssas
      ewprog.sas'
    4. all of the above

  10. 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?

    1. You misspelled a keyword.

    2. You forgot to end the DATA step with a RUN statement.

    3. You specified an invalid data set option.

    4. Some data values weren't appropriate for the SAS statements that you specified.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset