What you have here is legal.
If you have lots and lots of states
and/or groups, the local macro
created by -levelsof- may not
include all the names.
At that point you'd have to
fall back on the tricks documented in
4/03 Is there a way to tell Stata to try all values of a
particular variable in a foreach statement without
specifying them?
http://www.stata.com/support/faqs/data/foreach.html
Nick
[email protected]
A. Mobarak
> Dear Nick,
>
> Thank you very much for the quick response. Regarding your
> comment about
> the group variable, the 'group' number varies and 'states'
> are sometimes
> repeated across groups, so I will need the loop to run across groups.
>
> I am a newbie to Stata programming (sadly, I took data out to
> Matlab to
> program in the past), but can I simply add commands like the following
> to the program you kindly wrote for me?
>
> gen sumlength = .
> levelsof state, local(S)
> levelsof group, local(G)
>
> qui foreach g of local G {
> qui foreach s of local S {
>
> ...
> }
> }
>
> Thank you again. I will grateful (again) for a "reply all" rather than
> "reply".
Nick Cox
> gen sumlength = .
> levelsof state, local(S)
>
> qui foreach s of local S {
> su min if state == `s', meanonly
> local min = r(min)
> su max if state == `s', meanonly
> local max = r(max)
> su length if inrange(n, `min', `max'), meanonly
> replace sumlength = r(sum) if state == `s'
> }
>
> Whether the variable -group- complicates
> your problem is not clear.
*
* 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/