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

st: RE: forvalues problem


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: forvalues problem
Date   Thu, 1 Aug 2002 08:44:58 +0100

Fred Wolfe wrote

> Can anyone see the problem?
>
> I have global macros
>
> mac list
> catvar1_totline: 2
> catvar2_totline: 9
>
> When I try to use them with the following code I get an error
>
>        forvalues j = 1 /  2 {
>           forvalues k = 1 / ${catvar`j'_totline} {
>           di "`k'. ${catvar`j'vname`k'"
>           }
>        }
>
> The error is:
>
> - forvalues k = 1 / ${catvar`j'_totline} {
> unrecognized command:  catvar1_totline
>
> Leaving the braces off gives "invalid syntax."
>
> . di ${catvar1_totline}
> 2
>

Zhiqiang Wang wrote

> Your codes appear fine to me.  I haven't checked the Stata manual. I
guess
> that -foreach- may not get along well with glabol macro -{}-. To
avoid this,
> change the lines as follows:

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

My analysis is along the same lines. Think of

forvalues ... {

as being the command Stata knows about.
Once a command is identified, Stata will then substitute
local and global macros. Stata is trying to understand this as

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

which is evidently wrong in your case. (In principle, you _could_
have a program of your own with the name catvar1_totline.)

A quite different comment is that it's not clear
why you need the { } around the macro name in that position.

Finally, in

di "`k'. ${catvar`j'vname`k'"

the { is unmatched.

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/



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