![]() |
From | Christoph Birkel <christoph.birkel@soziologie.uni-halle.de> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Programming Problem: How to prevent macro substitution |
Date | Wed, 16 Jul 2008 19:13:57 +0200 |
Nick, thank you.
I was looking for a solution that can be done independently of the
creation of the temporary variables, since this is how Christoph
started the thread. What I didn't realise is that my solution will
contain the interior double quotes, which a simple -display- will not
reveal:
. local test "hello " "hello"
. di "`test'"
hello hello
. di `"`test'"'
hello " "hello
It is therefore clearly not suitable, and plainly wrong. Thanks for
pointing it out to me. Austin gave a perfect, working solution.
I also agree that escaping macro substitution is often not the way
forward. At least I have managed to avoid it completely in my work.
Eva
2008/7/16 Nick Cox <n.j.cox@durham.ac.uk>:
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
substitution.
For example, suppose you are in a loop
Within some loop {
...
tempvar foo
local xlist `xlist' i.land|`foo'
...
}
Otherwise put, I don't think there is ever a problem for which lines
like
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.)
Nick
n.j.cox@durham.ac.uk
Eva Poen
Thanks, Nick.
However, your suggestion does not produce the desired result:
. di "`xlist'"
i.land|ldep0 i.land|ldep1 i.land|ldep2
While Christoph was after:
. di "`xlist'"
i.land|`ldep0' i.land|`ldep1' i.land|`ldep2'
Unless I am missing something here.
Eva
2008/7/16 Nick Cox <n.j.cox@durham.ac.uk>:
I agree. In fact, I'd go one step further:Eva Poen
foreach u of numlist 0/2{
local xlist `xlist' i.land|ldep`u'
}
Sometimes the quotes just make things more complicated.
take it in steps and it works:i.land|`ldep0'
foreach u of numlist 0/2{
local xlist "`xlist' " "i.land|" "`" "ldep`u'" "'"
}
-di "`xlist'"- returns
i.land|`ldep0' i.land|`ldep1' i.land|`ldep2'
on my machine.
2008/7/16 Christoph Birkel <christoph.birkel@soziologie.uni-halle.de>:
I want to program a loop which produces a macro `xlist' containing alist of
dummy variable interaction expansion expressions with temporaryvariable
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' i.land|``ldep`u'''"
}
The resulting macro should contain an expression like "
arei.land|`ldep1' i.land|`ldep2' " (when `y2' contains 2) whichcan be
used as argument in -xi: reg-, as in: xi: reg yvar i.land `xlist' .(The
temporary variables `ldep0' and so on, corresponding to the macros
thegenerated later in a separate loop.)
What actually happens, is that, due to the single quotation marks,
bemacros containing the names of the temporary variables are subsitutedby
their content (which should not happen), so the content of theresulting
macro is " i.land|ldep0 i.land|ldep1 i.land|ldep2 ", which cannot
used*
to refer to temporary variables. For the same reason, it is notpossible to
define a local macro with a string as content which starts and endswith
quotation marks. I found no way to avoid the unintended macrosubstitution
using "\" or compound quotes. Any recommendation is highlyappreciated.
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
© Copyright 1996–2025 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |