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

RE: st: Programming Problem: How to prevent macro substitution

From   "Nick Cox" <>
To   <>
Subject   RE: st: Programming Problem: How to prevent macro substitution
Date   Wed, 16 Jul 2008 17:30:09 +0100

I see. Well, there will be an almost equally simple way of doing that,
or an equivalent, and one which certainly won't entail escaping macro

For example, suppose you are in a loop 

Within some loop { 
	tempvar foo 

	local xlist `xlist'|`foo' 

Otherwise put, I don't think there is ever a problem for which lines

	local ldep`u' "ldep`u'"

are part of the solution. 

(We had a similar thread a while back in which someone asserted that it
was sometimes necessary to delay substitution, and I kept disputing
that. It's a myth that seems to grow out of trying to translate other
programming styles to Stata macro programming.) 


Eva Poen

Thanks, Nick.

However, your suggestion does not produce the desired result:

. di "`xlist'"|ldep0|ldep1|ldep2

While Christoph was after:

. di "`xlist'"|`ldep0'|`ldep1'|`ldep2'

Unless I am missing something here.


2008/7/16 Nick Cox <>:

> I agree. In fact, I'd go one step further:
> foreach u of numlist 0/2{
>  local xlist `xlist'|ldep`u'
> }
> Sometimes the quotes just make things more complicated.

Eva Poen

> take it in steps and it works:
> foreach u of numlist 0/2{
>  local xlist "`xlist' " "|" "`" "ldep`u'" "'"
> }
> -di "`xlist'"- returns
> on my machine.
> 2008/7/16 Christoph Birkel <>:
>> I want to program a loop which produces a macro `xlist' containing a
> list of
>> dummy variable interaction expansion expressions with temporary
> variable
>> names `ldep0' , `ldep1' etc. as a string . For this purpose I wrote:
>> foreach u of numlist 0/`y2'{
>>  local ldep`u' "ldep`u'"
>> local xlist "`xlist'|``ldep`u'''"
>> }
>> The resulting macro should contain an expression like "|`ldep0'
>>|`ldep1'|`ldep2' "  (when  `y2'  contains 2)  which
> can be
>>  used as argument in -xi: reg-, as in: xi: reg yvar `xlist' .
> (The
>> temporary variables `ldep0' and so on, corresponding to the macros
>> generated later in  a separate loop.)
>> What actually happens, is that, due to the single quotation marks,
>> macros containing the names of the temporary variables are subsituted
> by
>> their content (which should not happen), so the content of the
> resulting
>> macro is "|ldep0|ldep1|ldep2 ", which cannot
> used
>> to refer to temporary variables. For the same reason, it is not
> possible to
>> define a local macro with a string as content which starts and ends
> with
>> quotation marks. I found no way to avoid the unintended macro
> substitution
>> using "\" or compound quotes. Any recommendation is highly
> appreciated.

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index