Statalist


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

st: RE: Re: How to get a macro with content `something'


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: How to get a macro with content `something'
Date   Wed, 27 Jan 2010 15:24:45 -0000

That's quite correct, naturally, and I'd forgotten about -macval()- when
I wrote earlier, although very occasionally I have used it. 

It remains true, I think, that most attempts to delay macro
substitutions arise from a misunderstanding of what is best needed
and/or are not in the programmer's best interests. That's certainly true
in this thread when as Alex has made very clear the felt need arose from
a different kind of problem, itself easily solved in another way. 

Nick 
[email protected] 

Kit Baum

This private exchange with Alan Riley, with regard to a problem I was
having with the -post- command,  might be illuminating:

(cfb)
> But what if what I wanted to post
> was the local macros exp1, exp2, exp3? I could not figure out a way
> (even with deferred macro substitution) to create a macro to_post
> which would itself contain the string
> 
> (`exp1') (`exp2') (`exp3')

(arr)
Here is what you could do:

 local i 1
 local list "(\`exp`i'')"

and then in a loop which increments `i':

 local list "`macval(list)' (\`exp`i'')"

The key is the use of `macval(<macroname>)' which substitutes the
contents
of the macro name passed to it as an argument _without recursive
substitution_.   That is, it only expands <macroname> at the top level,
and does not look inside it for other macro expansion characters.


On Jan 27, 2010, at 2:33 AM, Nick wrote:

> Dereferencing occurs at all levels as soon as you write down one or
more
> pairs of ` ' to indicate a local macro. It's not a question of peeling
> one layer off at a time. If that were so, working with nested macros
> would be more tedious. 
> 
> Thus although you inserted the text "`something'" inside local macro
A,
> as soon as you write 
> 
> `A' 
> 
> Stata looks inside, sees `something' and dereferences that in turn, so
> what you get is no surprise. 
> 
> If you don't want dereferencing, don't refer to a local macro (until
you
> need its contents, that is). 
> 
> You can delay dereferencing just once by using a backslash, but that
> doesn't wire in lifelong protection. It works just once, as said. 
> 
> As I don't recognise your need as one I've felt in 15 or so years of
> Stata programming, I am tempted to say you are not missing anything.
But
> let me ask directly: 

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