Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Re: Loop syntax


From   Johannes Geyer <[email protected]>
To   [email protected]
Subject   Re: st: Re: Loop syntax
Date   Thu, 27 Mar 2008 10:06:10 +0100

>  gen tcount=0
>  forvalues i=1/236{
>     forvalues j=1/57{
>     if patient==`i' & t>0 {
>     then tcount==tcount+1
>     continue
>  }
>  }
>  }

One question was, why this loop did not work. Vladimir gave the following 
answer:

> Talking just on a syntax matter, -then tcount==tcount+1- is incorrect.
> If you need just a number at the end, announce a local first before
> the loop and then increment this local within the loop:

> local tcount=0
> forvalues <...> {
>  if <...> {
>  local ++tcount
>  }
> }
< di `tcount'

I understand that this (a local) is more efficient than creating a 
variable but I cannot see the mistake. I guess the loop does not work as 
it is expected because "if" is used as a command and not as a qualifier: 
http://www.stata.com/support/faqs/lang/ifqualifier.html . 
The loop would look like this if you used "if" as a qualifier:

 local tcount=0
 forvalues i=1/236{
 forvalues j=1/57{
 local ++tcount if patient==`i' & t>0
 continue
 }
 }
 }

--
@Nick: What is the difference between -total()- and -sum()- that makes the 
latter better than the other?


Johannes


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



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