Statalist


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

RE: st: not sure about my loop results


From   Howard Lempel <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: not sure about my loop results
Date   Sun, 21 Dec 2008 17:48:02 -0500

Nirina,

If you want to avoid using 11 as a magic number, you should be able to do the following:

sum sibnum
local maxsib = r(max)

and then run your loop from 1 through `maxsib'

Hope this helps.
Howie

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of David Moore
Sent: Sunday, December 21, 2008 12:43 PM
To: [email protected]
Subject: RE: st: not sure about my loop results

Nirina,

Along with the advice of Gabi Huiber, you need to understand the effect of
your initial statement:

. local n=sibnum

This statement sets the value of the local variable `n' to the value of the
variable sibnum for the FIRST CASE in your dataset.  It does not update the
value of `n' for every case.  In short, the statement is executed ONCE for
the entire dataset.  I would be surprised if the value of sibnum for that
first case is not 5.

This is one of the two most frequently encountered gotchas of new (and some
of us old) Stata programmers.  The other is misunderstanding the role of
"if" as a conditional execution phrase in a Stata command and as a
conditional for Stata programs.

In any case, eliminate this line, alter the code to loop over 11 (not `n'),
perform the check Gabi recommended and you're home free, I believe.

Dave

. . . . . . . . . . . . . . . . . . 
David Moore, Ph.D.  |  Sr. Director
T I N D E R B O X  |  IGNITE. INSPIRE. INNOVATE. 
TINDERBOX IS PART OF THE HARTMAN GROUP, INC.
425.452.0818 ext 112  |  www.tinderboxthg.com


-----Original Message-----
Date: Sat, 20 Dec 2008 10:38:55 -0500
From: "Nirina F" <[email protected]>
Subject: st: not sure about my loop results

Hello,

I was writing the following loop to get the age of each of the
respondent's sibling.

local n=sibnum
forvalues i=1(1)`n'{
gen agesib`i'= 2006-yearsib`i' if yearsib`i' !=.
}

Nirina




*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index