# st: RE: RE: Complicated counting

 From "Moore, Jesse" To Subject st: RE: RE: Complicated counting Date Mon, 14 May 2007 15:56:46 -0400

```Worked beautifully, thanks so much.

JSM

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Monday, May 14, 2007 15:43
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: Complicated counting

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/

Confidentiality Notice:
This message, and any attachments, may contain information that is confidential, privileged, and/or protected from disclosure under state and federal laws that deal with the privacy and security of medical information. If you received this message in error or through inappropriate means, please reply to this message to notify the Sender that the message was received by you in error, and then permanently delete this message from all storage media, without forwarding or retaining a copy.

*
*   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/
```