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

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

From   Kit Baum <[email protected]>
To   [email protected]
Subject   st: Re: How to get a macro with content `something'
Date   Wed, 27 Jan 2010 10:08:06 -0500

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

> 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')

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: 

Kit Baum   |   Boston College Economics & DIW Berlin   |
                              An Introduction to Stata Programming  |
   An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:

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