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

From |
Shehzad Ali <sia500@york.ac.uk> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
RE: st: AW: simple sum() question |

Date |
15 Apr 2009 18:06:31 +0100 |

The solution I am now using is: bysort patient_id: egen sum_clinic = sum(clinic) Thank you, Shehzad On Apr 15 2009, Nick Cox wrote:

Unless there are further complications as yet unrevealed,bysort id : gen visits = _Nis a direct and simple solution.If you just wanted to count a subset, thengen interesting = <binary variable defining interesting>bysort interesting id : gen interesting_visits = _N if interestingThere are -egen- routes as well, but for problems like this going backto basics is difficult to beat.See alsoSJ-2-1 pr0004 . . . . . . . . . . Speaking Stata: How to move step by: step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox Q1/02 SJ 2(1):86--102 (no commands) explains the use of the by varlist : construct to tackle a variety of problems with group structure, ranging from simple calculations for each of several groups to more advanced manipulations that use the built-in _n and _N if a tutorial is needed. That's free on-line at the Stata Journalwebsite.Note that even if you did want a collapsed dataset, -contract- ratherthan -collapse- is more direct.Nickn.j.cox@durham.ac.ukShehzad Ali Hi Martin and Josiane, Thank you for your replies. You are right that I am interested in thetotalcount of visits for each patient and not the running sum.Sorry, I should have mentioned that patients who had three visits, forinstance, have three observations, and those with two visits have twoobservations. Therefore, the total number of observations for 100patientsis less than 400 (I had made up hypothetical numbers in haste tosimplifythe case. Not always a good idea).With Martin's solution, I will need to have four observations for eachpatient (sorry this was my fault as I didn't provide the correctinformation). With Josiane's suggestion, the dataset collapses which isnotwhat I want.Can you suggest a modified solution please? Again, sorry for the unclear email earlier. On Apr 15 2009, Martin Weiss wrote:I am betting that you want a count of visits, not a running sum, butcorrect me if I am wrong...clear* set obs 400 egen float patient = seq(), from(1) to(400) block(4) egen float visit = seq(), from(1) to(4) block(1) //not strictly necessary xtset patient visit //less than 4 visits for some replace visit =. if runiform()<0.05 bys patient: egen overallvisits=count(visit) l in 1/20, sepby(patient) noo *************Shehzad AliI have a simple question about summing across observations. I have 100patients (variable: patient_id) in the dataset, each had clinic visits(variable: clinic) and hospital visits (variable: hospital) recorded atweeks 4, 8, 12 and 16. The dataset is long and hence I have 400observations (one observation per patient per time point).I want to sum the clinic visits for each patient (across all 4 visits)bearing in mind that some patients had less than 4 visits. SoeffectivelyI want to generate a new variable that will produce the sum of clinicvisits for each patient.* * 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/

* * 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**:**Re: st: AW: simple sum() question***From:*"Martin Weiss" <martin.weiss1@gmx.de>

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

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

- Prev by Date:
**Re: st: RE: Data Management** - Next by Date:
**RE: st: AW: simple sum() question** - Previous by thread:
**RE: st: AW: simple sum() question** - Next by thread:
**RE: st: AW: simple sum() question** - Index(es):

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