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: foreach loop


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: foreach loop
Date   Sun, 5 Aug 2012 04:44:38 +0100

Look again at the allowed syntaxes for -foreach- in the help.

You can go

foreach x in 1/4 {

but then the next line will be interpreted as

codebook ... if m_marks == 1/4

which is illegal.

You could go (e.g.)

local X 1 2 3 4
foreach x of local X {

or

foreach x of num 1/4 {

but you won't get any unpacking of a list of values with

foreach ... in ... {

as it takes the elements of your list as you type them and 1/4 is a single element that is illegal as applied.

See also my tutorial in SJ 2(2): 202-222 (2002).

Nick


On 5 Aug 2012, at 03:03, Lisa Wang <[email protected]> wrote:

Hi all,

Can someone please teach me as to why this is invalid:

local x 1 2 3 4
foreach x in 1/4 {
codebook a* if m_marks==`x'
tab a* if m_marks==`x'
}

I have been learning Stata for only a month, so any suggestions would
be greatly appreciated. Or how to do it more elegantly would be great
as well.

I have 4 categories for m_marks only and I wanted to use a local to
loop over the codebook and tab functions for each category, instead of
typing it out 4 times.




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