How to Add Comments in Stata Do File

To save your future self (and your coauthors) a whole lot of frustration in handling and understanding do files, it is recommended that you add comments in Stata do file; comments that describe the purpose of the written code, as well as comments that serve as headings and titles for your do file and different pieces of code. Any text in green colour in a do file indicates that it is a comment. These comments do not get executed by Stata. This article will also discuss the use of line breaks at the end.

There are three ways to add comments in Stata:

  1. Double forward slash – //
  2. Asterisk – *
  3. A forward slash and an asterisk – /* comment */

Double Forward Slash

Anything written right after the double forward slash gets treated as a comment if it is in the same line as the forward slashes.

This can also be used to comment out actual Stata commands. For example, in the following example, the option of clear is commented out and will not be executed.

sysuse auto //, clear

Always add a space before the double forward slash, otherwise Stata treats them as part of the command and attempts to (unsuccessfully) execute them. The following is the wrong way to use the double forward slash:

sysuse auto, clear//This is a comment incorrectly added.

The correct way to write the above is:

sysuse auto, clear //This is a comment correctly added.

Asterisk

Asterisks provide limited commenting capability in that they only work if added at the very beginning of a line. You cannot add them in front of a command like we used the double forward slash.

sysuse auto, clear *This is an incorrectly written comment and will return an error.

Asterisk comments can be used interactively, which means they can be written directly in Stata’s command box and displayed in the Result window. Comments with double forward slash cannot be executed from the command box.

*This is a comment that can be entered through Stata’s command box or the do file.

A Forward Slash and an Asterisk

Any text encased between a forward slash and asterisk will be treated as a comment. Not only that, this style of commenting also allows you to take your comment to a new line. You can therefore comment out multiple lines of text or code as one comment.

/*This is the first line of this comment.
This is the second line of this comment.
sysuse auto, clear
The command above is part of the comment and will not be executed.
This is the last line of the comment.
*/

This commenting style can be used in the beginning, middle or end of a command or line.

sysuse auto /*, clear
The clear option is commented out, as is this line.
Like double forward slash, they can be used at the
beginning , middle or end of a line.
*/

Line Breaks

Oftentimes, our line of code gets very long and we have to resort to scrolling left and right to read it entirely. This is an inconvenience that interrupts the smooth reading of a do file. We cannot use the enter key to move part of our command to a new line since Stata considers the new line as a new command.

In version 13 of Stata, to create a line break in a long command, we use three forward slashes:

reg price mpg rep78 headroom trunk weight lengt ///
turn displacement gear_ratio

The above command is treated as one line of code because the three forward slashes indicate that anything written in the next line is still part of the first line.

Version 16 of Stata is much more responsive. It wraps the command in a new line if the current window size is too small for its entire length. This feature can be switched off from the ‘View’ drop down menu and unchecking the option called “Wrap lines”

There is also another way of taking our long line of code onto the next line. For this, we use the #delimit command and specify a character that will identify the end of a command. Most people use the semicolon (;) as a delimiter

#delimit ;

The above command tells Stata that anytime a semicolon (;) appears at the end of a line, it should end the command.

#delimit ;
reg price mpg rep78 headroom trunk weight lengt
turn displacement gear_ratio;

In the code above, the two lines are considered as one command. The semicolon at the end of the second line signifies the point where the reg command ends.

To revert back to Stata’s default settings, just enter the following command:

#delimit cr.

#delimit cannot be used interactively.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin