Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: loop with global marco


From   Maarten Buis <maartenlbuis@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: loop with global marco
Date   Wed, 19 Feb 2014 13:37:14 +0100

--- On Wed, Feb 19, 2014 at 1:23 PM, Nick Bornschein wrote:
> I defined 3 global macros, each of them containing the names of variables:
> (... means there are a lot more variables in it)
>
> global var1 "hope truth..."
> global var2 "life death..."
> global var3 "never loose..."
>
> Now I want to summarize the global macros in blocks with:
>
> foreach x of global var1 var2 var3 {
> su `x'
> }
>
> But it does not work, it always runs through all variables without creating
> blocks.

Here is how I would do this:

*------------------ begin example ------------------
sysuse auto
global var1 foreign rep78
global var2 mpg weight
global var3 trunk length

forvalues i = 1/3 {
    sum ${var`i'}
}
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )


This is not exactly true, I tend to avoid global macros wherever I can:

*------------------ begin example ------------------
sysuse auto
local var1 foreign rep78
local var2 mpg weight
local var3 trunk length

forvalues i = 1/3 {
    sum `var`i''
}
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index