Often in accounting/ financial research, we need count the number of firms in certain industries or analysts covering the firm.

A simple way to count the number of observations in Stata is using the “_N” command, which stands for total number in certain condition.

Let use our auto.dta

sysuse auto.dta

**Count the number of domestic/ foreign models

bysort foreign: gen n_models=_N

***this will yield 52 domestic models and 22 foreign models