Stata Help

The "by" Prefix

Using the "by" prefix is like running the command that follows, but several times with the data grouped by the another variable. For instance, I could look at the means and standard deviations in reaction times grouped by substance addiction before running any analyses on data. The full command would be by substance: summarize rtime

By will not work if the data is not already sorted by the variable you wish to separate the data by. However, if you have not yet sorted the data, you can add a sort to the by. Adding sort to the above example would look as follows: by substance, sort: summarize rtime If you forget to sort, Stata will display a "not sorted" error.

Finally, you can add secondary sort options, which can be useful if you have a repeated measure For example by ID (time), sort: [command] which would sort the data first by subjectID and then by time

Note: You cannot use the in suffix after a by prefix. The if suffix is allowed and can be manipulated to act the way an "in" statement does.
Back to Tutorials

Back to Syntax