# RE: st: how do i increment a counter

 From "Nick Cox" To Subject RE: st: how do i increment a counter Date Wed, 31 Aug 2005 22:33:11 +0100

```As others have advised, no loops are needed
for a problem like this. I guess at

gen age_category = cond(age <= 50, 1,
cond(age <= 75, 2,
cond(age <= 90, 3, 4)))

tabstat exposure_time, s(sum) by(exposure_time)

Nick
n.j.cox@durham.ac.uk

aine dooley

> What I am actually trying to do is loop through a list of
> 6500 patients.
> Each patient has an exposure_time i.e. the period of time in
> days during
> which they are at risk for a fracture. For each age category
> (see below), I
> want to add up a cumulative time at risk. So for ages 51-75,
> I want to come
> out with a number that contains the total exposure_time for
> all patient in that age category.
>
> I think numlist apparently only allows 1600 observations
> which is somehow
> incremented by using numlist, max('totobs`) though I haven't
> figured out the
> syntax.
>
> I'm sure there is a more elegant way to do this, however ...
>
> local age1_sum=0
> local age2_sum=0
> local age3_sum=0
> local age4_sum=0
> local totobs = _N
>
> foreach obs of numlist 1/`totobs' {
>    if age[`obs']<=50 {
>       local age1_sum = `age1_sum' + exposure_time[`obs']
>    }
>    else if age[`obs'] >50 & age[`obs'] <= 75 {
> 	      local age2_sum = `age2_sum' + exposure_time[`obs']
>    }
>    else if age[`obs'] >75 & age[`obs'] <= 90 {
> 	      local age3_sum = `age3_sum' + exposure_time[`obs']
>    }
>    else local age4_sum = `age4_sum' + exposure_time[`obs']
> }
>
>

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