Comment statement

From sasCommunity
Jump to: navigation, search
This is a work in progress. You can contribute to this article.

The comment statement can be used to include descriptive comments in SAS code. The purpose of comments is to provide documentation about the code. Comments are ignored when the code is parsed and do not affect how the code is executed.

Syntax

* <comment text (except for semicolon character)> ;
COMMENT <comment text (except for semicolon character)> ; - (Undocumented)

or in-line comments

/* <comment text (except for a asterisk-slash character combination)> */

or macro statement comments

%* <comment text (except for semicolon character)> ;

Usage

Comments can be used to comment out sections of code, by inserting an asterisk at the start of each statement, or by placing the in-line comments around the code. If code is commented out using in-line comments then the comments end at the first asterisk-slash character combination (*/) encountered, so in-line comments cannot be commented out a second time, rather the commenting out needs to be redone after each in-line comment ends.

Errors caused by poorly formed comments can be the source of silent and mysterious errors.

Because comments are transparent to the compiler, malformed comments do not produce syntax errors if code is inadvertently included in a comment. Omitting the semicolon from the end of a comment that is started with an asterisk can have the unintended effect of commenting out the following statement. For example, if a comment starting with an asterisk is added after a statement, then the following statement will be commented out if a terminating semicolon is not added to the end of the comment. Rather, the comment will continue to the semicolon at the end of the subsequent statement. Omission of the subsequent statement from the program may, or may not, cause a syntax error or run-time error.

Also be aware that in-line comments are ignored by the parser. This includes the /* and */ delimiters as well as everything between them. This means it is potentially possible for an in-line comment to be placed in the middle of a token without causing a syntax error. With using just an asterisk (*) to comment out a statement, this is less likely, because the error only propagates to the next semicolon.

Tracking down errors caused by comments can be time consuming. To assist in this effort SAS code editors often highlight comments in a different color from SAS code. However, even editors can be fooled on some occasions.

Example

/* Testing Comments */
OPTIONS mprint;
COMMENT This source code demonstrates the way comment statements are treated can depend on the code context;
 
DATA _NULL_;
 * %put This macro statement has been commented out.; 
 PUTLOG 'this SAS statement has NOT been commented out.';
RUN;
 
%macro nocomment;
 DATA _NULL_;
  * %put This macro statement has NOT been commented out!; 
  PUTLOG 'this SAS statement HAS BEEN COMMENTED OUT!!!';
 RUN;
%mend nocomment;
 
%nocomment;

Running this code produces the following log:

23         /* Testing Comments */
24         OPTIONS mprint;
25         
26         DATA _NULL_;
27         * %put This macro statement has been commented out.;
28         PUTLOG 'this SAS statement has NOT been commented out.';
29         RUN;
 
this SAS statement has NOT been commented out.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
 
30         
31         %macro nocomment;
32         DATA _NULL_;
33         * %put This macro statement has NOT been commented out!;
34         PUTLOG 'this SAS statement HAS BEEN COMMENTED OUT!!!';
35         RUN;
36         %mend nocomment;
37         
38         %nocomment;
MPRINT(NOCOMMENT):   DATA _NULL_;
This macro statement has NOT been commented out!
MPRINT(NOCOMMENT):   * PUTLOG 'this SAS statement HAS BEEN COMMENTED OUT!!!';
MPRINT(NOCOMMENT):   RUN;
 
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
 
39

The reason for the difference between the two DATA steps is that the first DATA step is parsed as SAS code while the second DATA step is parsed as macro code.

What goes in comments?

See Glossary for Comments

See also