Bysort command in Stata

May 18, 2019

The bysort command in Stata is used as a prefix before other commands, and it allows you to perform those commands within groups of observations. For example, it can be used to calculate the mean of a variable within categories of another variable. The bysort command is a perfect tool for this.

Using the nlsw88 dataset, let’s look at how to calculate the average wage for women either southerers or non-southerners. Here’s how you would do it:

sysuse nlsw88, clear
bysort south: summarize wage

Bysort example

From this example we can see that the mean wage is lower for southerners than for non-southerners.

bysort can be used with many other commands as well. For instance, if you want to list all observations within each category sorted by wage:

bysort south (wage): list

This will first sort observations based on the south variable, and then within each category of south, it will sort based on wage and list all variables for all women in the study.


Profile picture

Written by Johan Osterberg who lives and works in Gothenburg, Sweden as a developer specialized in e-commerce. Connect with me on Linkedin

2024 © Johan Osterberg