Statalist The Stata Listserver


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

st: Counting Instances of a value within a group


From   "Rachel S" <academicgirl@gmail.com>
To   statalist <statalist@hsphsun2.harvard.edu>
Subject   st: Counting Instances of a value within a group
Date   Tue, 12 Sep 2006 11:45:45 -0400

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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index