Statalist The Stata Listserver


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

st: RE: Using local variables in the name of a global variable


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Using local variables in the name of a global variable
Date   Fri, 17 Mar 2006 15:29:55 -0000

First off, terminology: not variables, but macros. 

Let's play "you are Stata". What do you see? 

You see 

di                             display command
"                              open quote 
$                              global macro name coming 
gooddaysY                      global macro name? 
			no such name, oh well show nothing 
`                              local macro name coming 
y                              local macroname? 
			yes, show it 
'                              end of local macro name 

etc. 

OK, you're not Stata any more. (It's hard work, you have
to go so slowly....) 

You need { } to tell Stata how your stuff is to be parsed. 

forval y = 1980(1)2006 {
    forval m = 1(1)12 {
           
        di "${gooddaysY`y'M`m'} "
    }
}

That way stuff is nested properly. This is in the manual
somewhere. 

Nick 
n.j.cox@durham.ac.uk 

Danielle H. Ferry
 
> I want to define a global variable whose name contains 2 
> local variables (b/c
> I am creating it w/in nested loops over year & month):
> 
> forval y = 1980(1)2006 {
>     forval m = 1(1)12 {
>         global gooddaysY`y'M`m' = "stuff"
>         di "$gooddaysY`y'M`m'"
>     }
> }
> 
> Suppose we are in the y = 1980 & m = 1 loop. Then the result 
> of the -display-
> is: 1980M1. I am guessing this has something to do w/ the 
> quotes. But I don't
> know how to fix it. Any ideas?

*
*   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–2020 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index