# Re: st: coding question

 From "Ziad El-Khatib" To statalist@hsphsun2.harvard.edu Subject Re: st: coding question Date Mon, 3 Mar 2008 12:52:37 -0800

```Hi Maarten,
You did kindly answer to this query before, i meant to confirm if i
want to calculate the cumulative value for first 6 visits only, would
the command be:

bys id (visitn) : gen cumulad = ///
sum(pillmissed)/sum(pillspres) * 100, if visitn>=1&visitn<=6

Thank you in advance and best regards

On Wed, Jan 30, 2008 at 12:04 AM, Maarten buis <maartenbuis@yahoo.co.uk> wrote:
>  > Question how to calculate variable representing cumulative value
>  >
>  > = [(total n pillmissed per patient at all visits) / (total n
>  > pillspres at all visits)] * 100
>
>  The trick is to combine -bys- with the -sum- function. -bys id
>  (visitn)- does whatever folows the : for each person separetely and the
>  (visitn) ensures that within each person the cases are ordered by
>  visitn. The -sum- function gives you the cumulative sum. So combining
>  the two gives you:
>
>  *-------------- begin example ------------------
>  drop _all
>  input long id visitn pillspres pillmissed
>
> 1001  1   30  2
>  1001  2   20  5
>  1001  6   50  1
>  end
>
>  bys id (visitn) : gen cumulad = ///
>     sum(pillmissed)/sum(pillspres) * 100
>
>  list
>  *--------------- end example --------------------
>  (For more on how to use examples I sent to the Statalist, see
>  http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )
>
>  Hope this helps,
>  Maarten
>
>  -----------------------------------------
>  Maarten L. Buis
>  Department of Social Research Methodology
>  Vrije Universiteit Amsterdam
>  Boelelaan 1081
>  1081 HV Amsterdam
>  The Netherlands
>
>  Buitenveldertselaan 3 (Metropolitan), room Z434
>
>  +31 20 5986715
>
>  http://home.fsw.vu.nl/m.buis/
>  -----------------------------------------
>
>
>
>       ___________________________________________________________
>  Support the World Aids Awareness campaign this month with Yahoo! For Good http://uk.promotions.yahoo.com/forgood/
>
>
> *
>  *   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/
*
*   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/
```