Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Small macro - char problem


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Small macro - char problem
Date   Mon, 8 Jul 2002 10:47:27 +0100

Fred Wolfe
> 
> I have a small macro - char problem.
> 
> I define the following:
> 
> char totinco[outname] "Total Income (\$US)"
> local outname: char totinco[outname]
> 
> Now I display the char and macro.
> . char l totinco[outname]
> totincom[outname]:          Total Income ($US)
> 
> . di "`outname'"
> Total Income ()
> 
> Notice the $US is omitted in the macro but not the char. I have tried 
> various ways to get it to display, but without success. Any suggestion?

This looks at first sight like a small bug, but I don't think it is. 
I believe that Stata is only being completely consistent with 
its own rules. 

To backtrack, and to expand for the benefit of others, you want the 
literal dollar character, but Stata is predisposed to treat 

$US 

as a reference to a global macro named 

US 

so the remedy is to use \ as an escape character 
to get the literal meaning, as documented at 
[U] 21.3.8. 

However, the act of putting the characteristic 
in a local macro strips the escape character 
-- as is shown by -char li- -- 
and what ends up in the local macro is just the literal 
$ character. 

Thereafter -display- is necessarily ignorant 
of the previous incarnation of your local macro.   
It just sees, apart from unproblematic characters, 

$US

and tries to interpret that as a global macro. 
As, evidently, no such macro has been defined by you, 
Stata is substituting an empty string. 

The work-around is to use the SMCL incantation 
of {c S|}, as is documented at [P] smcl: this 
example is discussed on p.275. 

Another comment is that, for the code you present, 
you don't need the local any way. You can display 
a characteristic directly: 

di "`totinco[outname]'" 

Nick 
[email protected] 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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