Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: local variable within a global macro?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: local variable within a global macro?
Date   Tue, 29 Aug 2006 14:20:02 +0100

The previous posting explained that the 
terminology "variable" is not appropriate
here. 

Your main question was answered earlier this month 
in this posting:

=============================================
st: RE: Variable names incompatable with aflogit?
Date: Fri, 11 Aug 2006 20:03:23 +0100

The problem is with references to 

$`1' 

where Stata is expected by the programmer 
to take the local macro name first and then 
evaluate the global name that results. If that 
is what you want, you are expected to spell that 
out explicitly. 

Presumably this worked at one point, but 
(again I surmise) StataCorp perhaps tightened 
up on what was seen as a bug or a misfeature. 

A quick experiment indicates that changes 
to the code so that these lines read as below 
produces output. 

212     replace `1' = ${`1'} - `1'
241     gen `zmx' = ${`1'} - `1'
272     replace `1' = ${`1'} if `1' != .
281     replace `1' = ${`1'} if `1' != .        /* reset exposure to reference level */

That is, assignments referring to 

$`1' 

should be to 

${`1'} 

instead. 
=============================================================

In your case, your code would be better off as 

forvalues n = 1/10 {
	local p = `n' + 1
	reg y`n' ${X`p'}
}

which forces Stata to delay evaluation of the global 
macro name until it has evaluated the local macro 
name. 

But look at your code. You are looping over n
and also over p, by an explicit increment. 

That's because you have your global macro 
numbering out of sync with your y numbering. 
I would sort that out upstream, so that
you can just go 

forvalues n = 1/10 {
	reg y`n' ${X`n'}
}

The reason is that if your numbering is awkward for one bit 
of code, it is going to be awkward for other
bits too. All this code is going to be harder to 
write, harder to read, and more prone bugs 

If there is some compelling argument not 
to do what looks dopey on the evidence available, then 
note another way to do it: 

forval n = 1/10 { 
	reg y`n' ${X`=`n' + 1'}

but I recommend tidying up over trickery every 
time. 

Nick
[email protected] 

Nishant Dass

> I have a small (or so it seems) problem with a loop (shown
> below).  
> 
> I start by generating a few global macros:
> global X2 "age2 weight2 height2"
> global X3 "age3 weight3 height3"
> ...
> global X11 "age11 weight11 height11"
> 
> Then I try to run the following set of regressions using a
> loop:
> forvalues n = 1/10 {
> 	local p = `n' + 1
> 	reg y`n' $X`p'
> }
> 
> However, this isn't executed due to an error that says "2
> invalid name" (i.e., it's referring to some problem in the
> way I have used `p' in $X`p'.)
> 
> Could anyone please tell me what would be the correct way
> to do this?  

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