Re: st: If.....

 From "Nick Cox" To Subject Re: st: If..... Date Mon, 2 Sep 2002 11:39:19 +0100

```Chris.Fergusson@sth.nhs.uk

> I need to set up an if loop but am having trouble with the syntax.
I want
> to calculate the upper and lower 95% confidence intervals for the
> standardised incidence rate of a specific cancer over several years.
There
> are two ways of calculating these upper and lower bounds depending
on how
> many registered cases there were of that particular cancer with the
> benchmark set at 500.  I have the total number of registrations per
year
> setup as a variable (total) in the datasheet as follows
>
> year total
> 1985 353
> 1986 427
> 1987 359
> 1988 363
> 1989 356
> 1990 364
> 1991 326
> 1992 318
> 1993 338
> 1994 323
> 1995 329
> 1996 318
> 1997 313
> 1998 318
> 1999 317
> 2000 309
>
> I also have a matrix (reg) setup which contains one column containg
the same
> number as the variable total
>
> reg[16,1]
> total
> r1    353
> r2    427
> r3    359
> r4    363
> r5    356
> r6    364
> r7    326
> r8    318
> r9    338
> r10    323
> r11    329
> r12    318
> r13    313
> r14    318
> r15    317
> r16    309
>
> What I need to know is how to open the if loop i.e.
> if .....<500 {

if ... {
...
}

selects on a single true-or-false condition and probably should not be
used
for this purpose. An FAQ expands on this detail:

http://www.stata.com/support/faqs/lang/ifqualifier.html

It may be that what you want is more like this:

. gen ... if reg < 500
. replace ... if reg >= 500

Alternatively, it is possible to loop over observations

forval i = 1/16 {
if reg[`i'] < 500 {
...
}
else {
...
}
}

In general, this is very slow. In any case, it can usually be
avoided.

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

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