Statalist The Stata Listserver


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

Re: st: RE: local variable within a global macro?


From   Nishant Dass <[email protected]>
To   [email protected]
Subject   Re: st: RE: local variable within a global macro?
Date   Tue, 29 Aug 2006 07:34:13 -0700 (PDT)

Dear list-members,

Thank you very much for all your suggestions.  I must say,
joining this list has been a good learning experience.  

For the sake of it, I tried all the solutions suggested and
most worked. 

Nick's solution: Using ${X`p'} worked well.  (As regards
the awkward numbering - in my present scenario, it's easier
for me to use it as such; moreover, I was interested in
knowing how to use local macros within global macros, and
the current problem provided a good opportunity, so I kept
the numbering as it is.)

Kris' solution: Defining my globals as locals instead (I
would call them global macros and local macros but I am not
sure; perhaps, Nick can clarify) and then writing `X`p''
worked, too.

Alex's solution was right in principle but involved a small
typo.  Instead of:
      local globalname "X`np1'"
      reg y`n' $X`globalname'
it should be
      local globalname "`X`np1''"
      reg y`n' `globalname'

And finally, Prabhu was right, too - Kris' solution doesn't
work if I retain the global macros. 

Thank you very much, everyone.  

Regards,

Nishant
 


--- Nick Cox <[email protected]> wrote:

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


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   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