Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: Re: Macro escape inside a macro


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Re: Macro escape inside a macro
Date   Wed, 29 Sep 2010 11:23:35 +0100

Michael is of course correct. 

-display-'s mission in life is to evaluate, calculate and display. Thus it sees 

"`x'" 

which in turn means 

"``d''"

and as local macro d is undefined the whole thing evaluates to an empty string. (First `d' evaluates to an empty string, and then `' evaluates to an empty string. Just as in algebra with nested parentheses or brackets, Stata works from the inside going outward.) 

Otherwise put, you can get `' as literal characters into a macro, but once you refer to that macro Stata has a built-in rule that it should interpret `string' as a reference to a local macro named -string-. 

-macval()- can be found used in -log2html- from SSC. 

My prior probability that Alexsander does not need to do what he is trying to do I will write down as 0.9. 

Nick 
n.j.cox@durham.ac.uk 

Michael N. Mitchell


   I think that the -display- command is fooling you about the contents of the macro -x-. 
Consider this example (based on yours), but instead I will use the -macro list- command in 
addition to -display-.

* Assign the macro to x
.   local x "\`d'"

* Now see what it contains
. macro list
(some output omitted)
_x:             `d'

Note that _x is the way of -macro list- saying that x is a local macro and it contains `d'.

* Now use the -display- command, it looks empty
. display "`x'"


The display command shows us the contents of -"`x'" which then resolves to "`d'" which 
then resolves to nothing since their is no macro named `d'. So, let's create such a macro.

. local d This is the macro d

* Now what does macro list show

. macro list
(some output omitted)
_d:             This is the macro d
_x:             `d'

Note now what happens when we display "`x'"...

. display "`x'"
This is the macro d

   The macro "`x'" is resolved to contain "`d'" which is then resolve to contain "This is 
the macro d" which is then displayed.

   I suspect that this still may leave you short of solving your problem. If so, perhaps 
you could post more about the exact problem you are facing so we can help further.

On 2010-09-29 1.19 AM, Aleksander Rutkowski wrote:

> Eric, Nic, thank you for your replies. It appears that the beginning
> of my original message did not reach statalist. I am resending it --
> see below. BTW, I have read
> http://www.stata.com/support/faqs/lang/backslash.html but it shows
> only simple actions with -display- command, which work fine, but my
> problem is more complicated. I want to delay substitution of a macro
> which is nested in another macro -- see below. Why backslash works
> when used with -display- but not with -local-? (In my example below I
> want to expand x but not d). I tried to use -macval()- but I did not
> succeed. The description of -macval()- in the [P] manual is very brief
> and there is no example with a full syntax. Maybe somebody could give
> an example how -macval()- should be used?
>
> -- original message below --
>
> Why:
>    di "\`d'"
> displays:
>    `d'
> but:
>    local x "\`d'"
>    di "`x'"
> displays empty string?
>
> How can I escape macro (i.e. prevent macro substitution) inside
> another macro (i.e. when defining another macro)?
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index