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]

st: RE: loop question


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: loop question
Date   Tue, 30 Nov 2010 16:55:27 +0000

First off, a -foreach- loop over the integers 1/20 is better written as a -forval- loop 

forval n = 1/20 { 

} 

But more importantly, 

1. Looping sounds a bad way to do what you want.  

2. You are treating macros as if they were variables, which is a major misunderstanding.

You'd be better off explaining the structure of your data and what you want to do in words, and then you'll probably get better advice on what you should do. 

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

P.S. please see Statalist advice: 

http://www.stata.com/support/faqs/res/statalist.html#tojoin

especially 2.1.3. 

P C
===

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. Otherwise, "i" won't increase 
by 1 and will jump out of the loop.

How should I write the loop?


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