Presentation on theme: "Debugging SAS Programs"— Presentation transcript:
1 Debugging SAS Programs Finding and CorrectingErrors
2 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.
3 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 and SAS could not execute 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.Some Notes are very important!
4 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.
5 Missing SemicolonsMissing semicolons are the most common mistake to make.From Program 4, if:DATA weight;INFILE 'C:\SAS_Files\tomhs.dat';Is replaced with:DATA weight
6 One Missing Semicolon Produced: ERROR: No DATALINES or INFILE statement.ERROR: Extension for physical file name "C:\SAS_Files\tomhs.data" does not correspond toa 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 therewere 0 observations and 8 variables.WARNING: The data set WORK.INFILE may be incomplete. When this step was stopped there
7 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.
8 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:TABLES sex clinic
9 The Missing Semicolon Produced: ERROR: Variable TITLE not found.ERROR : Syntax error, expecting one of the following: a name, ;, (, *, -, /, :,_ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.ERROR : The symbol is not recognized and will be ignored.
10 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!
11 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:INFILE 'C:\SAS_Files\tomhs.data;
12 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.;850 INFILE 'C:\SAS_Files\tomhs.data;49NOTE : 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.
13 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 : 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.
14 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).
15 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.
16 Invalid DataIf SAS is expecting a number, but gets text instead, you can get invalid data notes.From Program 5, if:@ clinic $1.Is replaced with:@ clinic
17 One Missing $ Produced: NOTE: The infile 'C:\SAS_Files\tomhs.dat' is:File Name=C:\SAS_Files\tomhs.dat,RECFM=V,LRECL=256NOTE: Invalid data for clinic in lineRULE:C C 11/10/ /26/ /17/ /25/ptid=C03615 clinic=. group=5 sex=1 educ=4 evsmoke=2 alcbl=0 sebl_1=1 sebl_6=1 _ERROR_=1_N_=1
18 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:VAR clinic group sex educ sebl_1 sebl_6;
19 Using the Wrong Syntax Produced: 1015 PROC FREQ DATA=tdata;VAR clinic group sex educ sebl_1 sebl_6;---180ERROR : Statement is not valid or it is used out of proper order.Note: Similar errors can be produced by missing semicolons
20 Misspelled Variable in a PROC From Progam 4, if:PROC FREQ DATA=weight;TABLES sex clinic ;Is replaced with:TABLES sex clinc ;You get:ERROR: Variable CLINC not found.
21 Uninitialized Variables From Program 4, if:bmi = (weight* )/(height*height);Is replaced with:bmi = (wieght* )/(height*height);You get:NOTE: Variable wieght is uninitialized.
22 What’s an Uninitialized Variable? An uninitialized variable is a variable that SAS considers to be nonexistent.This usually occurs when a variable name on the RHS of an equation is misspelled.In the example, the error was caused by a misspelling—SAS had no variable called wieght.
23 Forgetting the RUN Statement If you forget the RUN statement at the end of you program, SAS will not run (on PC)You won’t get any output.You may not get any errors or warnings.Fix: Run a single RUN; statement.
24 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.
25 Multipart ProgramsIf 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.
26 Example: DATA weight; INFILE 'C:\SAS_Files\tomhs.dat'; ptid clinic age sex 1.@58 height weight cholbl;bmi = (weight* )/(height*height);RUN;/*PROC FREQ DATA=weight;TABLES sex clinic ;TITLE 'Frequency Distribution of Clinical Center and Gender';*/TABLES clinic/ NOCUM ;TITLE 'Frequency Distribution of Clinical Center ';TITLE2 '(No Cumulative Percentages) ';*Now SAS will only perform the second PROC FREQ;
27 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;
Your consent to our cookies if you continue to use this website.