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

Re: st: forvalues problem


From   [email protected] (Vince Wiggins, StataCorp)
To   [email protected]
Subject   Re: st: forvalues problem
Date   Thu, 01 Aug 2002 13:19:15 -0500

Fred Wolfe <[email protected]> notes that Stata produces an error
message when the following commands are run:


	global catvar1_totline 2
	global catvar2_totline 9

	forvalues j = 1 /  2 {
		forvalues k = 1 / ${catvar`j'_totline} {
			...
		}
	}

and that it is the second -forvalues- statement that produces the error,

	forvalues k = 1 / ${catvar`j'_totline} {

This should work, but it doesn't.  It is the multiple open braces '{' that are
confusing -forvalues- and the problem is related to making -forvalues- work
with commands follow the '{', e.g.,

	forvalues i=1/10 { display `i' }

Suffice to say that we cannot make Fred's construct work without breaking some
of currently allowed constructs using -forvalues-.  -forvalues- can and will
be made to understand Fred's construct, but this can only be done under
version control so that all of the currently allowed constructs continue
working under with appropriate -version- statements.

For now, anyone encountering the problem can use either Nick Cox's
<[email protected]> solution employing local macros (though I suspect Fred
is using global macros to communicate across programs in his application),

	local catvar1_totline 2
	local catvar2_totline 9

	forvalues j = 1 /  2 {
		forvalues k = 1 / `catvar`j'_totline' {
			...
		}
	}

or, Zhiqiang Wang's <[email protected]> solution that removes the
brace-bound global macro expansion from the -forvalues- command,

	forvalues j = 1 /  2 {
		local newnum ${catvar`j'_totline}
		forvalues k = 1 / `newnum' {
			...
		}
	}


-- Vince
   [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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index