Statalist


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

st: RE: display a local macro mixing text and numbers


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: display a local macro mixing text and numbers
Date   Mon, 5 Jan 2009 23:03:43 -0000

You'd be confused too if you were Stata. Or rather you wouldn't be clear what Jacob Wegelin wants of you, as his last statement is ambiguous. 

More positively, I don't think Stata programmers ever stop getting bitten in this territory. You just get used to fixing the problem when Stata spots it before you do. 

Here as always the key is to try to see what Stata sees after substitution of macro names by their contents. 

The problem with 

. local junk 5 "99" oops 4.4

. di "junk is " "`junk'"

is that Stata is trying to make sense of 

"junk is " "5 "99" oops 4.4" 

Now, the problem with that is the ambiguity of the double quote ".  

Consider something simpler: 

"a "b" c" 

What's the pairing here? Is this one quoted string nested within another or two disjoint quoted strings with a name in the middle? As opening and closing double quote marks are exactly the same, Stata can get confused with examples like this. 

So, Stata introduced compound double quotes `" "' so you can disambiguate. 

. di "junk is " `"`junk'"'
junk is 5 "99" oops 4.4

produces what I think you want. The `" "' makes it clear that the " " inside the macro named junk belong to each other. Just as at many social gatherings, things are often much easier to understand when you know which the couples are! 

Nick 
[email protected] 

Jacob Wegelin

In the code below, why does Stata return an error?  I run into this kind of problem repeatedly.  Although I have explicitly told Stata to include "99" as a string in the macro, Stata appears to get confused.

. local junk "99"

. di `junk'
99

. di "`junk'"
99

. 
. local junk 5 99 oops 4.4

. di "junk is " "`junk'"
junk is 5 99 oops 4.4

. local junk 5 "99" oops 4.4

. di "junk is " "`junk'"
junk is 5 4" invalid name
r(198);

*
*   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