[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Ulrich Kohler <kohler@wzb.eu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: simple sum() question |

Date |
Fri, 17 Apr 2009 15:42:56 +0200 |

Shehzad Ali wrote > Here is a quick summary of what I am doing. Each patient (varname: > patient_id) was observed at 4 time points and at each time point we asked > about the clinic visits (varname: clinic) in the last 3 months. The dataset > is in long form (shown below): > > patient_id timepoint clinic > 1 1 0 > 1 2 1 > 1 3 . > 2 1 2 > 2 2 0 > 3 1 1 > 3 2 . > > The line below generates a sum of all clinic visits for each patient: > > bysort patient_id: egen sum_clinic = sum(clinic) > > Now if at one time point, clinic visit is missing (as its seen for patients > 1 and 3), then I want stata to return missing value for the sum. The above > command returns the total of the non-missing observations, ignoring the > missing ones (understandably). But if I tried: > > bysort patient_id: egen sum_clinic = sum(clinic) if clinic!=. > > then it returns missing value for the sum variable only for the time point > which is missing and not for all the time points for that patient. Can > anyone please suggest how to resolve this? Martin already gave a good solution. Here is an alternative starting from first principles: . by patient_id (clinic), sort: gen sum_clinic = sum(clinic) . by patient_id (clinic): /// replace sum_clinic = cond(!mi(clinic[_N]),sum_clinic[_N],.) This uses the fact that observations which are missing on clinic==. are sorted at the end of each observation. > Secondly, whats the best way to collapse the dataset to one observation per > patient? Once I have the sum_clinic for each patient, it would be easier > just to have one observation per patient. by patient_id: keep if _n==1 * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**AW: st: simple sum() question***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**st: Re: rnormal()***From:*"vmz@vol.net.mt" <vmz@vol.net.mt>

**References**:**st: simple sum() question***From:*Shehzad Ali <sia500@york.ac.uk>

**st: AW: simple sum() question***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**Re: st: AW: simple sum() question***From:*Shehzad Ali <sia500@york.ac.uk>

**RE: st: AW: simple sum() question***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**RE: st: AW: simple sum() question***From:*Shehzad Ali <sia500@york.ac.uk>

**Re: st: AW: simple sum() question***From:*"Martin Weiss" <martin.weiss1@gmx.de>

**Re: st: simple sum() question***From:*Shehzad Ali <sia500@york.ac.uk>

**AW: st: simple sum() question***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**st: AW: Running the suest command on an unbalanced panel** - Next by Date:
**st: Re: rnormal()** - Previous by thread:
**AW: st: simple sum() question** - Next by thread:
**st: Re: rnormal()** - Index(es):

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