Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: loop question


From   Sergiy Radyakin <[email protected]>
To   [email protected]
Subject   Re: st: loop question
Date   Tue, 30 Nov 2010 12:58:19 -0500

On Tue, Nov 30, 2010 at 11:37 AM, P C <[email protected]> wrote:
> Hi,
>
> I have a loop question.
>
> I want to do something like this:
>
> local i=0
> gen w1ttflsbyger=.
> foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
>  replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
> w1ageint~=.
>  replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
> }
>
> Of course, the last line of the syntax was wrong. Basically, I want to write
> something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
> & w1flsibage`n'!=. & w1ageint~=." are satisfied.

if (w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.) local i=`i'+1

Also use -forval- instead of -foreach- (if such loop is at all
necessary), if you
miss 13 in the above list, you will have a hard time finding it.

Also you would typically want to avoid this:
w1ageint~=.

Write intstead: !missing(w1ageint)
It is easier to read, and makes sure that you treat extended missings
same as the
system missing. How do you treat them in your current code?

Best, S.R.

> Otherwise, "i" won't increase
> by 1 and will jump out of the loop.
>
> How should I write the loop?
>
> Thanks!
>
>
>
>
> *
> *   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index