# st: RE: Complicated counting

 From "Nick Cox" To Subject st: RE: Complicated counting Date Mon, 14 May 2007 20:42:58 +0100

```bysort ProviderId Hospital : gen count = _n == 1
by ProviderId : replace count = sum(count)
by ProviderId : replace count = count[_N]

OR

bysort ProviderId (Hospital) : gen count = sum(Hospital != Hospital[_n-1])
by ProviderId : replace count = count[_N]

OR

egen tag = tag(ProviderId Hospital)
egen count = total(tag), by(ProviderId)

OR

The beast is canned as -egen, nvals()- in -egenmore- from SSC.

egen count = nvals(Hospital), by(ProviderId)

AND see

FAQ     . . . . . . . . . . . . . . . . . . .  Number of distinct observations
. . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and G. Longton
4/02    How do I compute the number of distinct observations?
http://www.stata.com/support/faqs/data/distinct.html
Nick
n.j.cox@durham.ac.uk

Jesse Moore MD

> This is perhaps a mundane question (compared to some posts), but
> important to me nonetheless:
> My data looks like this:
> ProviderID    Hospital
> A                    1
> A                    1
> A                    1
> A                    1
> B                    1
> B                    1
> B                    2
>
> I would like to create a variable with the number of
> hospitals that each
> provider has worked at.  For provider A it would be 1, for
> provider B it
> would be 2.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```