Group contiguous observations with identical values of a varlist
cgroup varlist , generate(newvarname)
cgroup inputs a list of variables specified by a varlist, and generates, as output, a new integer-valued variable, indicating, in each observation, membership of a group of contiguous observations with identical values of the varlist. It is therefore a non-sorting version of gsort.
generate(varname) is required, and specifies the name of an output variable to be generated. This output variable will contain, in each observation, the group to which that observation belongs. It will have 1 integer value per contiguous group of observations with identical values for the variables in the varlist, in ascending order of appearance in the dataset, starting with 1.
The following example demonstrates the use of cgroup with the sencode package, downloadable from SSC.
.sysuse auto, clear .gene firm=word(make,1) .cgroup foreign firm, gene(firmseq) .sencode firm, replace manyto1 gsort(firmseq) .tab firm, missing
The following example demonstrates the use of cgroup with the group() function of egen. Alternatively, we could have used the xgroup package, downloadable from SSC.
.sysuse auto, clear .gene firm=word(make,1) .cgroup foreign firm, gene(firmseq) .egen firmord=group(firmseq foreign firm), label .tab firmord, m
Roger Newson, National Heart and Lung Institute, Imperial College London, UK. Email: email@example.com
Manual: [D] egen, [D] gsort On-line: help for egen, gsort help for sencode, xgroup if installed