A Stata program to count clusters

By Tony Brady

If you haven't already done so, you may find it useful to read the article on xtab because it discusses what we mean by longitudinal data.

xcount is a simple program, similar to count, except it counts clusters instead of observations. The clusters must be identified either by using the i() option, or previously with the iis command.


To follow this example in Stata type:

use http://www.sealedenvelope.com/stata/long.dta

in the Stata command window.

For instance, to count the number of patients in our example dataset (long.dta) we can use either:

. iis idnum
. xcount


. xcount, i(idnum)

Either way, we get the following output:

xcount output

So there are 15 patients in this dataset. Contrast this to the result from count:

xcount output

To count the number of male patients:

number of males

To count the number of patients whose systolic blood pressure has exceeded 160mmHg at some point during the follow-up period we use:

sbp output


To obtain xcount type the following into Stata:

net from http://www.sealedenvelope.com/

and follow the instructions on screen. This will ensure the files are installed in the right place and you can easily uninstall the command later if you wish.