Using Comments Smartly

Comments can be more useful than you think. While the commands tell Stata what to do, the comments remind ourselves or the readers what we have been doing.

Adding comments

Depending on one's preference, there are three ways to indicate comments in Stata.

*this is a comment
//this is a comment
/* this is a comment */

//this is a comment and /* this is a comment */ are used in do-files.

Anything commented by // and /* */ will not be executed.

Annotating codes

We use comments to annotate our codes, reminding ourselves of why we did one thing and helping our collaborators understand what we did.
. sum length if price > 10000 & !missing(price) //make sure only nonmissing cases are used.

Commenting out commands

/* */ can be used in the middle of the commands to comment out the commands. For instance,
. list make if price > 10000 /*|rep78 <= 2 */ if we are testing alternative codes and not sure yet if we want to run the piece of codes in /**/.

Making long commands readable

To make our long commands readable, use /// to tell Stata that the command continues to the next line and everything following the continuation lines is part of the commands.
. sum length if price > 10000 ///
& rep78 <= 2 ///
& make !="Audi Fox"

Starting a do-file properly

We may want to start our do-files with the information of title, author, date etc. using comments.
* project
* author, date

Dividing long scripts

Comments can divide our codes into different parts to make long scripts more readable.
**************
//model1
some commands

**************
//model2
more commands

Author: Yun Dai, 2018