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

st: RE: Mixing global macros with local macros


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Mixing global macros with local macros
Date   Wed, 9 Oct 2002 16:50:48 +0100

mganz
> 
> I'm running Stata 6.0 and I want to set some global 
> macros in one program and then access them, in a 
> general way in another.
> 
> This is what I want to do:
> 
> * In the main program I set the globals
> global skip1=1
> for num 2/10: global skipX=0
> do something

This can slow a program down. -while- is 
going to be faster than -for- for this. 
(In 7, -forval- is even faster and better.) 

> 
> * Then in the something.do file I want to repeatedly
> * do some regressions and simulations, but only if
> * a $skip macro is not equal to 1
> local i=1
> while (`i'<=10){
> 	if $skip`i'~=1{
> 		reg y x w z, if b==`i'
> 		* etc, etc
> 	}
> 	local i=`i'+1
> }
> 
> But when the while-loop expands the $skip`i' it doesn't 
> expand it to $skip1, $skip2, etc.
> 
> How can I accomplish this in a generalized and hopefully
> not too cumbersome way?

The problem is that Stata is reading from left 
to right and dividing your command into tokens. 
By default the ` of `i' is taken as a signal 
that a new token is starting, and so necessarily 
that current token, which started with $skip,
is ending. Thus Stata treats your line as 

$skip 

followed immediately by 

`i' 

Very likely, $skip is undefined and so 
evaluates to an empty string. `i' is 
of course well defined. 

You just have to flag to Stata to substitute `i' 
before it works out the global: 

${skip`i'} 

That way, Stata does things in the right order, 
just as with parentheses, brackets and braces
in an algebraic expression, except that Stata
uses braces for this specific purpose. 

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