How to add Comments in SAS (7 Proven Ways)

The comments are an important part of the programming. Either you’re writing a few lines of code or maybe writing a complicated, lengthy program, you must document important information within the code using comments. 

So even when you return to that code after months or years, you or anyone else can easily understand the logic written by just reading out the comments.

Also maintaining a change log at the beginning of the code is highly-highly recommended. 

Over the period of time there would be lots of changes you do in the program so having a change log gives you more clarity on when, what and who made changes in the program. You can find the well structured change log comment template here.

There are multiple ways you can add comments in SAS or comment out an already written SAS statement. Here are 7 proven ways you can add comments in SAS code.

7 Proven Ways to Add Comments in SAS

1. /* Write your comment here */

This is the easiest and safest way to add comments in SAS code or comment out SAS statements. You can use keyboard shortcut (CTRL+*) on SAS Studio to get this comment line.

/* This is a comment in SAS (Shortcut: Ctrl + * ) */

2. * Write your comment here ;

This is the simplest comment you can write. Remember to end your comment with a semicolon. This option is easy but gets tricky if you try to comment SAS statement which has characters like %, &, or “”, etc.

* This is a comment in SAS;

3. Line break or Header

This is very similar to the previous option but with many asterisks. You can draw a line,  or add a comment break which looks like a heading  in the program.

*********This is a comment in SAS*******;

4. Multi-line Comment 

This is useful when you want to add multiple lines of comments or you want to comment out multiple lines of SAS code.

For the keyboard shortcut, select lines that you want to comment out and press (Ctrl + *). You can select same lines again and press Ctrl + * to uncomment those lines.

/* Select and press "Ctrl + *" to comment and uncomment  */

/* Select and press "Ctrl + *" to comment and uncomment  */

/* Select and press "Ctrl + *" to comment and uncomment  */

/* Select and press "Ctrl + *" to comment and uncomment  */

 

There is another way you can multi-line comment by adding asterisk at the beginning of each line and ends with semicolon. 

*The is an example of multiline comments;

*This is a article about SAS comments;

*URL: https://learnSASCode.com/sas-comments/;

*Author: The SAS Programming Expert;

 

This is one more way to add multi-line comments which is even more popular because of its simplicity.

/*  The multiline comment:

This is a article about SAS comments

URL: https://learnSASCode.com/sas-comments/

Author: The SAS Programming Expert  */

5. Use * after % 

You can add * after % in the SAS statements to comment that line. For example,  %LET is a SAS statement that can be commented by adding an asterisk after %. 

You can also comment out the statement where SAS macro gets called so that macro can not be executed by SAS.

%*LET myvar=100;

%*macro_name ;

6. Add %* at the beginning 

When you’re dealing with lots of macros and SAS stored procedures then this option could be handy to comment out SAS statements. 

%* This is sample comment ;

%*%macro_name() ;

7. “COMMENT” Keyword

It’s a hidden but an interesting trick. There is a keyword “comment” available in SAS which you can use to write a comment in between the SAS statement.

Syntax:

comment write-your-comment-here ;

For the demonstration purpose we have added two comments using comment keywords. It doesn’t affect the SAS code execution, neither disturb the output. 

data test;
comment This is my first comment ;
dt=today();
comment This is my second comment ;
format dt date9.;
run;

The output doesn’t change:

SAS comment keyword log
SAS comment keyword

BONUS: SAS Program Change Log Template

This is a widely used template to maintain change log in the SAS program itself. You can just copy this template and make necessary changes.

comments in SAS code change log template

Copy this template:

***********************************************************
Program : Comments_in_SAS.sas
Description : This code demonstrate 7 proven ways to add
comments or comment out SAS statements.
Author : SAS Expert 1
Date : 05.08.2022
************************************************************;

************************************************************
CHANGE LOG:

Date : 15.09.2022
Changed by : SAS Expert 2
Changes : Bug fixes and added some more information
about point number 5

Date : 15.09.2022
Changed by : SAS Expert 3
Changes : Bug fixes and added some more information
about point number 1

Date : 15.09.2022
Changed by : SAS Expert 4
Changes : Bug fixes and added some more information
about point number 5

.
.
.

***************************************************************;


FAQ

How many ways you can add comments in SAS?

There are total 7 ways you can add- comments or comment out SAS statements.

  1. /* Write your comment here */
  2. * Write your comment here ;
  3. Line break or Header
  4. Multi-line Comment 
  5. Use * after % 
  6. Add %* at the beginning 
  7. “COMMENT” Keyword
Is it important to maintain change log in the program?

Yes, it is. In order to track when, what and who made changes in the code it recommended that to maintain change long in the SAS program itself. It’s a good programming practice to follow.