[SAS base] debuging SAS programs

Source: Internet
Author: User


SAS Program has three types of errors:

  • Programing logic errors (how to identify and resolve );
  • Syntax errors (how to recogize ANC correct );
  • Data errors (how to examine and resolve.

How to write an efficient SAS Program:

  • Write code that is easy to read as much as possible (one line per sentence, indented layout, and comments more)

  • Test any part of the program
  • Test Program with a small dataset (for example, using obs and other options)
  • Use a syntax-sensitive program editor (SAS keywords is a color, variable is another color; all the text enclosed by a bank is rendered in the same color)

Method for checking syntax errors

1 Options obs = 0 noreplace; 2/* 1) obs = 0 tell SAS not to import any data; 3) the noreplace option tells SAS not to use an existing SAS dataset instead of an empty one */4After confirming that there is no syntax error, you can change obs = 0 to OBS = max.


Missing semicolon error (datastmtchk System option)

(If a semicolon is lost, SAS combines the two statements to read them together, resulting in a prompt message for puzzling)


1 Options datastmtchk = allkeywords:/* All the keywords in the SAS command cannot be used as the data set name */2 Data toads/* missing a semicolon */3 infile 'C: \ myrawdata \ toadjump. dat '; 4 input toadname $ weight jump1 jump2 jump3; 5 run; 6 7 [log prompt]: Error: infile is not allowed in the data statement when option datastmtchk = allkeywords.8 9[Explanation] datasmtchk system options, which can be used as the name of a SAS data set in the Data step. This option defaults to merge, retain, set, or update, and cannot be used as the dataset name.


Note: input statement reached past the end of the line

1 infile 'C: \ myrawdata \ toadjmp2.dat 'missover; 2/* When the missover option command SAS reads data, if the modified data has been read, but the variable is still not read, assign missing values to the remaining variables */


[Possible error cause]

  • The data is like this.
  • Space at the beginning or end of the data (view the mininmum line length of the SAS log. If the value is 0, it indicates that there is a blank space and should be deleted)
  • List input is used, but some values are read as a value without spaces;
  • If some write data rows are wider than the remaining ones, column or formatted is still used for import. (The truncover option should be added after the infile Statement ).


Note: lost card

This prompt indicates that SAS reads the next row of data in expecting, but cannot find this row of data. For example, if each observation needs to read two rows of data, the SAS dataset should have an even number of rows. If there are only an odd number of rows, the log will prompt the lost card information.

Note: Invalid Data

The input statement of the read data field does not match. As a result, SAS cannot read data from the original data. This is a reflection that SAS may make: Assign the variable missing values; or print a prompt similar to probliematic observation in the log.

[Possible error cause]

  • It should have been a numeric-defined variable that has a balanced type of observed values, such as the number 0 and the letter O confusion;
  • The user forgets to define a variable as numeric, while the SAS system automatically defaults to numeric.
  • Incorrect column specifications may generate built-in spaces in numeric data;
  • In list-style input, two periods (...) are displayed simultaneously in the same row, but there is no space in the middle;
  • In list-style input, the missing value is not marked with periods (.), causing SAS to read the next data;
  • Special characters (such as the tab key) appear in numeric data;
  • Use the wrong sequence. For example, use mmddyy instead of ddmmyy .);
  • Invalid date. It is read in date format, for example, September 31.

[Remarks]: 1. When SAS encounters unprintable (for example, hexadecimal format), the message "invalid data" appears in the log;

2. Sometimes you need to run this program. To ensure _ error _ = 0, use ?? -- Format modifier:

1 INPUT IDNumber ?? 1-5 Name $ 6-18 Class $ 20-21 Q1 22 Q2 23;



Note: numeric values have been converted to character, vice versa

If you mix the numeric and character variables, SAS automatically attempts to convert numeric to character, or convert character to numeric, which will be reflected in the log.

[Converting variables]

  • Character is converted to numeric: newvar = input (oldvar, callback)

Just like using promise in an input statement, promise is used in the input function, and promise must be the numeric data type that you need to converting.

  • Numeric to character: newvar = put (oldvar, Format)

Just like using format in a put statement, format is used in the put function, and format must be the numeric data type that the user needs to converting from.

Error Type: The result of the Data step is incorrect, but no error message is displayed.

In fact, when the put statement is used, logs are not written together with the file statement. To solve this type of problem, the putlog statement should be used, which has the same function as the put statement. However, whether or not it is used together with the file statement, logs will be written.

1 * keep only students with mean below 70; 2 Data lowscore; 3 infile 'C: \ myrawdata \ class. dat '; 4 input name $ score1 score2 score3 homework; 5 homework = homework * 2; 6 averagescore = mean (score1 + score2 + score3 + homework ); 7 putlog name = score1 = score2 = score3 = homework =;/putlog _ all _;/* the value of each variable before the if statement is output */8 If averagescore <70; 9 run;


Error: statment is not valid

Description: SAS can't understand the statement at all.

[Possible cause]

  • Incorrect spelling keyword;
  • Incorrect semicolon (Chinese and English obfuscation );
  • Use statements that can only be used in the data step In Proc, and vice versa;
  • There is a run statement in the Data step or proc step;
  • The options statement is incorrect;
  • Quotation marks do not match
  • Comment Symbols do not match.


Error: variable is uninitalized or error: variable not found

Error: variable is uninitalized: It appears in the Data step. It indicates that the user needs to initialize the variable and re-execute the program;

Error: variable not found: It appears in Proc step. If the error appears in Var, SAS will prompt this information and then not execute the program. If the error appears in label, SAS prompts a warning and continues executing the command.

[Possible cause]

  • Incorrect spelling variable name;
  • Used variables that have been dropped before;
  • The dataset is not referenced correctly;
  • There is a logical error.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.