Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Counting Instances of a value within a group


From   "Austin Nichols" <[email protected]>
To   [email protected]
Subject   Re: st: Counting Instances of a value within a group
Date   Tue, 12 Sep 2006 12:00:06 -0400

bys ID: egen D1=total(Dose==1)

On 9/12/06, Rachel S <[email protected]> wrote:
I'm trying to figure out how to count the number of instances of a
value of a variable within a group.

The relevant subset of my dataset looks like

ID   Dose  Drug
11    1        A
11    2        B
12    2       A
12    .        B
13    1       A
13    1      B

I'd like to add two variables, Dose1 and Dose2, which count the number
of drugs for which a patient is taking a dose of 1 or 2, respectively.
 Each of these two new variables should be constant across
observations for a patient.

Dose can contain the number 1,2, or 3 or a missing value.

I used the commands:

by ID: egen Dose1=count(Dose) if Dose==1
by ID: egen Dose2=count(Dose) if Dose==2

However, this generates the variables Dose1 and Dose2 only for those
observations with the relevant dose.  That is, the "if" statement
seems to be attached to the egen command rather than the count
function.

How can I fix this?

Thanks,
Rachel
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index