Bookmark and Share

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

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

RE: st: use of brace inside of macro - still a question

From   "Sarah Edgington" <>
To   <>
Subject   RE: st: use of brace inside of macro - still a question
Date   Wed, 16 Jun 2010 10:56:53 -0700

It looks like the explanation you quote from Maarten below is exactly the
answer to your question.  The macros appear to not be expanded until Stata
starts actually doing the iterations, which it doesn't do until it confirms
that the loop ends.  From Stata's perspective the loop never ends because
there's no closing brace.  (Note also that there's a typo in your code.
$[test} should be ${test}.  This appears to be irrelevant to the outcome,

Do you have a compelling reason to want to do this?  I'm not sure I
understand why you would want to replace one character, a brace, with
multiple characters, a macro name.  Moreover from a proofreading perspective
not being able to visually see the closing brace on your loop seems like it
would make spotting errors, particularly when putting loops within loops,
harder, not easier.  And since closing braces always have to be on their own
line I can't see any situation where using a macro would ever saving you any

-Sarah Edgington

-----Original Message-----
[] On Behalf Of B Web
Sent: Wednesday, June 16, 2010 10:32 AM
Subject: Re: st: use of brace inside of macro - still a question

looking for and I apologize for not being specific.

I want to end a loop defined by a "brace" stored in a global macro.

macro drop _all
global test  }

forv i=1/2{
 di in r "${test}"
* --- error ---
What I am trying to do is "end" the loop using the global macro of a
brace.   Can you get that working?  If so, please explain.  If not -
can someone else please elaborate.


On Wed, Jun 16, 2010 at 12:32 AM, Maarten buis <>
> --- On Wed, 16/6/10, Dr. Bill Westman wrote:
>> I am curious whether a brace can be
>> used inside of a macro.
>> For example:
>> sysuse auto, clear
>> macro drop _all
>> global test  }
>> log using test, replace
>> forvalues j = 1/1  {
>> macro list _all
>> }
>> forvalues x = 1/1  {
>> macro list _all
>> ${test}
>> end
>> break
>> ...
>> causes Stata to provide r(1) error.
>> I've tried the macro with and without quotes "" around the brace.
> I guess what is happening is that Stata is collecting the lines of 
> code that are part of a -forvalues- block without evaluating the local 
> and global macros. This makes sense to me, as often you want the 
> values of those macros to change across iterations of that loop. The 
> consequence is then that you can't use a global or local macro 
> containing a brace to close the loop: Stata saw the begining of the 
> loop, so it starts collecting lines till it sees a closing brace, and 
> since it is not evaluating the macros it can't find the closing brace, 
> and than runs into your -break- command.
> Hope this helps,
> Maarten
> --------------------------
> Maarten L. Buis
> Institut fuer Soziologie
> Universitaet Tuebingen
> Wilhelmstrasse 36
> 72074 Tuebingen
> Germany
> --------------------------
> *
> *   For searches and help try:
> *
> *
> *

*   For searches and help try:

*   For searches and help try:

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