debug prog

27
Debugging SAS Programs Finding and Correcting Errors

Upload: rmayreddy

Post on 01-Feb-2016

268 views

Category:

Documents


0 download

DESCRIPTION

Debug Prog

TRANSCRIPT

Page 1: Debug Prog

Debugging SAS Programs

Finding and Correcting

Errors

Page 2: Debug Prog

Checking the Log

• It is always a good idea to check the log file.

• Start at the beginning of the log file, and correct the first error. Sometimes one mistake can create many errors.

Page 3: Debug Prog

Errors, Warnings, and Notes

• There are three kinds of notifications that SAS inserts into log files: Errors, Warnings, and Notes.

• An Error indicates that there was a problem in the program.

• A Warning indicates that there was a problem in the program, but SAS figured out how to continue.

• Notes can indicate that a program worked as planned or that a program worked differently.

Page 4: Debug Prog

Backwards Illustrations

• When your SAS programs don’t work the way that you want, you’ll have to figure out what went wrong.

• In this lesson, errors will be introduced intentionally to see the results.

Page 5: Debug Prog

Missing Semicolons

• Missing semicolons are the most common mistake to make.

• From Program 4, if:

DATA weight;

INFILE 'C:\SAS_Files\tomhs.data';

• Is replaced with:

DATA weight

INFILE 'C:\SAS_Files\tomhs.data';

Page 6: Debug Prog

One Missing Semicolon Produced:

ERROR: No DATALINES or INFILE statement.ERROR: Extension for physical file name "C:\SAS_Files\

tomhs.data" does not correspond to a valid member type.NOTE: The SAS System stopped processing this step

because of errors.WARNING: The data set WORK.WEIGHT may be

incomplete. When this step was stopped there were 0 observations and 8 variables.WARNING: The data set WORK.INFILE may be

incomplete. When this step was stopped there were 0 observations and 8 variables.

Page 7: Debug Prog

How to figure out what happened:

• The Error said that there wasn’t a DATALINES or INFILE statement, but you know that there was one.

• SAS must not have identified the INFILE statement as an INFILE statement.

• Checking the code shows that that SAS thought that the INFILE statement was part of the DATA statement because a semicolon was missing.

Page 8: Debug Prog

Another Missing Semicolon:

• From Program 4, if:PROC FREQ DATA=weight; TABLES sex clinic ; TITLE 'Frequency Distribution of Clinical Center

and Gender';• Is replaced with:PROC FREQ DATA=weight; TABLES sex clinic TITLE 'Frequency Distribution of Clinical Center

and Gender';

Page 9: Debug Prog

The Missing Semicolon Produced:

-------------------------------------------------------

22 200

ERROR: Variable TITLE not found.

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, (, *, -, /, :,

_ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.

ERROR 200-322: The symbol is not recognized and will be ignored.

Page 10: Debug Prog

How to figure out what happened:

• SAS says that the variable TITLE wasn’t found.

• You know that TITLE isn’t a variable.

• SAS must think that TITLE is part of a list of variables.

• There is no semicolon separating TITLE from the variables SEX and CLINIC!

Page 11: Debug Prog

Unbalanced Quotation Marks

• An Unbalanced quotation marks warning can indicate that a quotation mark is missing.

• From Program 5, if:

DATA tdata;

INFILE 'C:\SAS_Files\tomhs.data’;• Is replaced with:

DATA tdata;

INFILE 'C:\SAS_Files\tomhs.data;

Page 12: Debug Prog

One Missing Quotation Mark Produced:

WARNING: The quoted string currently being processed has become more than 262 characters long. You may have unbalanced quotation marks.

861 ;850 INFILE 'C:\SAS_Files\tomhs.data; ------------------------- 49NOTE 49-169: The meaning of an identifier after a quoted

string may change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended.

Page 13: Debug Prog

What if you Balance the Quotation and Run Again?

• You still get errors!

• SAS interprets your program as a continuation of the program it ran before. Since there is an unbalanced quote, your quotes are still unbalanced and you get the Note:

NOTE 49-169: The meaning of an identifier after a quoted string may change in a future SAS release. Inserting white space between a quoted string and the succeeding identifier is recommended.

Page 14: Debug Prog

The Fix: Another Unbalance Quote

• Run these two lines of code:‘

RUN;

• Do this ONCE (so the unbalance quote becomes balanced).

• You program should run properly now (as long as it is error-free).

Page 15: Debug Prog

Another Fix

• This may be easier to understand:

• First, correct the unbalanced quote.

• Second, save your SAS program.

• Third, exit SAS.

• Fourth, reopen SAS and run your saved program.

Page 16: Debug Prog

Invalid Data

• If SAS is expecting a number, but gets text instead, you can get invalid data notes.

• From Program 5, if:

@ 12 clinic $1.

• Is replaced with:

@ 12 clinic 1.

Page 17: Debug Prog

One Missing $ Produced:NOTE: The infile 'C:\SAS_Files\tomhs.data' is: File Name=C:\SAS_Files\tomhs.data, RECFM=V,LRECL=256

NOTE: Invalid data for clinic in line 1 12-12.RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----1 C03615 C 11/10/1987 5 51 1 1 06/26/1936 5 4 2 71.5 05/17/1988

11/25/ 80 1988 205.5 199.0 093 084 143 138 36 36 5 260 046

111 159 4.8 063 02213 45.6 9.3 46.4 00471 00711 03611 01906 0.0

00 1 238 1 1 1 2 1 1 1 1 1 1ptid=C03615 clinic=. group=5 sex=1 educ=4 evsmoke=2 alcbl=0 sebl_1=1

sebl_6=1 _ERROR_=1_N_=1

Page 18: Debug Prog

Mixing up PROCs

• Different PROCs have different options.

• From Program 5, if:PROC FREQ DATA=tdata;

TABLES clinic group sex educ sebl_1 sebl_6

• Is replaced with:

PROC FREQ DATA=tdata;

VAR clinic group sex educ sebl_1 sebl_6

Page 19: Debug Prog

Using the Wrong Syntax Produced:

1015 PROC FREQ DATA=tdata;1016 VAR clinic group sex educ sebl_1

sebl_6; --- 180ERROR 180-322: Statement is not valid or it

is used out of proper order.• Note: Similar errors can be produced by

missing semicolons

Page 20: Debug Prog

Misspelled Variable in a PROC

• From Progam 4, if:PROC FREQ DATA=weight; TABLES sex clinic ;• Is repleaced with:PROC FREQ DATA=weight; TABLES sex clinc ;• You get:ERROR: Variable CLINC not found.

Page 21: Debug Prog

Uninitialized Variables

• From Program 4, if:

bmi = (weight*703.0768)/(height*height);

• Is replaced with:

bmi = (wieght*703.0768)/(height*height);

• You get:

NOTE: Variable wieght is uninitialized.

Page 22: Debug Prog

What’s an Uninitialized Variable?

• An uninitialized variable is a variable that SAS considers to be nonexistent.

• In the example, the error was caused by a misspelling—SAS had no variable called wieght.

Page 23: Debug Prog

Forgetting the RUN Statement

• If you forget the RUN statement at the end of you program, SAS will not run.

• You won’t get any output.

• You may not get any errors or warnings.

Page 24: Debug Prog

Catching Errors as You Write:

• You don’t have to write an entire program, then run the whole thing.

• Try writing your programs in stages. – Write part and run it. – If your program works, write the next part, and

run it. – If your program produced errors or warnings,

it must have been from the last part that you wrote.

Page 25: Debug Prog

Multipart Programs

• If you are writing a program in stages, you may have multiple procedures. Running the same procedures over and over produces a lot of output and log files to check.

• Once you get a procedure to work, you can enclose it in a comment (/* . . . */) while you work on other procedures.

• Just remove the comment when you’ve finished the whole program.

Page 26: Debug Prog

Example:DATA weight; INFILE 'C:\SAS_Files\tomhs.data'; INPUT @1 ptid $10. @12 clinic $1. @27 age 2. @30 sex 1. @58 height 4.1 @85 weight 5.1 @140 cholbl; bmi = (weight*703.0768)/(height*height);RUN;/*PROC FREQ DATA=weight; TABLES sex clinic ; TITLE 'Frequency Distribution of Clinical Center and Gender';RUN;*/PROC FREQ DATA=weight; TABLES clinic/ NOCUM ; TITLE 'Frequency Distribution of Clinical Center '; TITLE2 '(No Cumulative Percentages) ';RUN;*Now SAS will only perform the second PROC FREQ;

Page 27: Debug Prog

Checking On Your Data Sets

• Sometimes your data steps don’t work the way you want, but there aren’t any clear indications of problems from the log file.

• You can insert a PROC PRINT to see your data:PROC PRINT DATA=mydata;RUN;• Then, when you’re sure that your data is OK,

you can either delete the PROC PRINT or convert it into a comment:

*PROC PRINT DATA=mydata;*RUN;