There are three types of comments: PL/1 style, asterisk style, and macro style.
1. PL/1 style comments are processed in the SAS word scanner, character by character.
/* This comment's bytes are processed character by character. */
2. Asterisk style comments are processed in the tokenizer, token by token, until a semicolon is reached. However, single and double quotation marks are treated as special tokens, so that string literal tokens are NOT built in this case.
* Asterisk comments are read token by token but quotes are treated like single tokens.;
(However, inside a macro definition, they are gathered as constant text. So the constant text is built with tokenization rules, including string literal tokens.)
3. Macro style comments are processed by the macro facility, token by token, until a semicolon is reached. Single and double quotation marks build string literals, so every open quotation mark must have a closing quotation mark.
%* This macro comment is read token by token until the semicolon.;
To understand the descriptions above, it is important to understand what a token is. A word or token in the SAS programming language is a collection of characters that communicates a meaning to SAS and which cannot be divided into smaller units that can be used independently. A word can contain a maximum of 32,767 characters.
A word or token ends when SAS encounters one of the following:
Each word or token in the SAS language belongs to one of four categories:
Look at a few examples of using these different styles of comments within a macro:
Example 1 (macro style comment):
%macro test(dsin);
%* Shouldn't the single quotation mark be ignored since comment?;
proc print data=&dsin;
run;
%mend;
data test;
i=1;
run;
%test(test)
The macro comment begins and we start processing tokens after the '%*'. The first token is received as "Shouldn". The word scanner looks for the next token or semicolon to end the comment. The word scanner finds a single quotation mark and begins to gather characters looking for the next single quotation mark in order to make a string literal. Starting with the single quotation mark, the following is read as part of the literal string:
't the single quotation mark be ignored since comment?;
proc print data=&dsin;
run;
%mend;
data test;
i=1;
run;
%test(test)
The word scanner is still looking for the closing single quotation mark. As you continue to submit code, the word scanner continues to read characters looking for a closing quotation mark. The maximum length of a character value is 32,767 bytes. However, if you reference a quoted string that is longer than 262 bytes, the following warning message is issued:
WARNING 32-169: The quoted string currently being processed has become
more than 262 characters long. You may have unbalanced quotation marks.
To fix this, use the PL/1 style comment instead.
Example 2 (asterisk style comment):
%macro one;
* This is a test %let x = this would not work;
data new;
set one;
run;
%mend;
%one
What happens is:
%ONE is executed:
ERROR 180-322: Statement is not valid or it is used out of proper order.
This error is generated because the semicolon that ended the DATA statement actually ended the asterisk style comment. This means that the DATA statement is now part of the comment leaving the SET statement out in open code.
In a macro definition, everything is compiled from the end of the %MACRO statement to the %MEND:
The important rule to remember here is that asterisk style and macro style comments are read token by token until a semicolon is found. PL/1 style comments are read character by character.
The safest comment to use inside a macro is the PL/1 style comments in the form of /* comment */. Since this style of comment is read character by character, special characters and macro triggers such as % and & do not cause a problem and are truly seen as a comment.