[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: Counting Instances of a value within a group
bys ID: egen D1=total(Dose==1)
On 9/12/06, Rachel S <firstname.lastname@example.org> 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
How can I fix this?
* For searches and help try: