A syntax error is a common SAS programming error.
It happens when the code includes incorrect syntax.
Below are some examples:
Let's look at each type of error in detail.
Spelling errors happen when a SAS keyword is misspelled.
Below is an example:
proc freqx data=sashelp.cars; table make; run;
The FREQ procedure above is misspelled as FREQX.
When you run the program, you will see the following error message in the log:
Spelling errors can be identified in the SAS log.
Simply review the error message on the SAS log and then correct the spelling error.
Missing Semi-colon Error
In SAS, each statement ends with a semi-colon.
Running the following statement without a semi-colon will cause an error:
proc freq data=sashelp.cars table make; run;
In the above example, we are missing the semi-colon at the end of the PROC FREQ statement:
What will happen if you omit the semi-colon?
SAS will treat the first and second line of your code as a single statement:
Essentially, you are running the code below:
SAS will treat the keyword "table" and "make" as options of the Proc Freq statement (similar to the NOPRINT, KEEP and DROP options).
Since TABLE and MAKE are not valid options for Proc Freq, SAS will generate an invalid option error on the SAS log:
ERROR 22-322: Syntax error, expecting one of the following: ;, (, COMPRESS, DATA, FC, FORMCHAR, NLEVELS, NOPRINT, ORDER, PAGE.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
When you see such error messages, you should check if you have:
We will look at the invalid options error shortly.
An unbalanced quote is another common error in SAS.
It happens when you miss a quotation mark when assigning character values (e.g. var1 = "ABC ;).
You can identify such errors by the unusual color pattern of the code.
Let's look at an example:
data test; a = "ABC; b = 123 ; run;
Copy and paste the code into the SAS editor.
You can can see that something is wrong by the color of the code:
In SAS, the character values within the quotation change to purple.
If any part of your code (not values) changes to purple, this indicates there is a missing quotation.
Let's add the missing quote back to the code:
data test; a = "ABC"; b = 123 ; run;
The colors now look normal.
Now, none of the code is purple, except for the literal character values.
Copy and run the code below in SAS:
What kind of errors can you identify from the code above?
Please provide the correct code.
Need some help?
Is there anything strange about the color of the code? Is there an error message?
The code contains a spelling error (i.e. Proc Prin), missing semi-colon (i.e. sashelp.cars) and an unbalanced quote (i.e. "Price;).
The first two can be identified in the error message in the SAS log. The last one can be identified by the color of the text.
The correct code is:
rename msrp = price;
proc print data=cars;
label price = "Price";