[email protected]
> I am running into problems when I try to "nest-loop" using
> the old "for"
> and, reluctantly, the new "forvalues".
>
> My aim is to have the following tabulation:
>
> for Y in num 1/6: for X in num 1/6: ta vnum mXachieved if site==Y &
> mvvdate~=., row \ ta vnum mXachieved if site==Y, row
>
> I get an error message I guess because the inside loop will
> only execute the
> first tabulation, comes to the second and can not find
> "mXachieved"!!
>
>
> If the command is modified to read:
>
> for Y in num 1/6: for X in num 1/6: ta vnum mXachieved if site==Y &
> mvvdate~=., row \ for X in num 1/6: ta vnum mXachieved if
> site==Y, row
>
> That yields the exact tabulation to breaking-up the loop
> into two separate
> processes as follows:
>
> for Y in num 1/6: for X in num 1/6: ta vnum mXachieved if site==Y &
> mvvdate~=., row
> for Y in num 1/6: for X in num 1/6: ta vnum mXachieved if
> site==Y, row
>
>
> I tried to use the new "forvalues" as follows:
>
> forvalues Y = 1(1)6 {
> 2. forvalues X = 1(1)6 {
> 2. ta vnum mXachieved if site==Y
> & mvvdate~=.,
> row
> 3. ta vnum mXachieved if site==Y, row }
> }
>
> I get an error message telling me:
>
> unrecognised command: 2. invalid command name
> r(199);
The way to do this with two -forvalues- loops is
forval Y = 1/6 {
forval X = 1/6 {
ta vnum m`X'achieved if site==`Y' & mvvdate <., row
ta vnum m`X'achieved if site==`Y', row
}
}
You don't need the line numbers. Also,
your syntax was wrong because you have not
used references to X and Y correctly within each
loop. You must refer to local macro X as `X' etc.
Incidentally, I changed the inequality
mvvdate ~= .
to
mvdate < .
In Stata 7, the two are equivalent. In Stata 8, they
are not, but the second is usually closer to what
you want. In any case, you save a keystroke every time
you use the second way.
There was a tutorial on -forvalues- and -foreach-
in SJ 2(2), 202-222 (2002) and a sequel in
SJ 3(2), 185--202 (2003). The gist of the first
is accessible at
http://www.stata.com/support/meeting/8uk/fortitude.pdf
Nick
[email protected]
*
* 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/