Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: repeating for groups


From   Christopher F Baum <[email protected]>
To   [email protected]
Subject   st: Re: repeating for groups
Date   Sun, 3 Aug 2003 09:41:17 -0400

On Saturday, August 2, 2003, at 02:33 AM, Dimitriy wrote:

sum group
scalar min=r(min)
scalar max=r(max)

local i=min
while `i'<=max {
display _newline(2) "group is `i'"
tab race if group==`i'
tab sex if group==`i'
tab race sex if group==`i'
local i=`i'+1
}

Scalar min and max are defined as the min and max of your group variable.
In your case, min is 1 and max is 50. You could have specified them
yourself instead of defining a scalar. The loop goes trough your group
variable and executes commands based for each group i.
This is better done with forvalues, as Fred Wolfe suggested. Unfortunately forvalues does not appear in the base Stata documentation, but it is well documented on-line and in FAQs. But this approach will fail as soon as one of the groups is missing (what if no one is in group 37)? In that case foreach, most handily driven by the new levels command, is the way to go. Although levels, originally written by Nick Cox, is now an (undocumented) component of Stata 8, it may be used by Stata 7 users with 'ssc install levels', which will provide levels7, with most of the same functionality.

Kit

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