Statalist


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

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


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: display a local macro mixing text and numbers
Date   Thu, 8 Jan 2009 19:32:21 -0000

Here is my attempt to summarize: 

Double quotes that bound a string by default are stripped: they are
string delimiters, not part of that string. 

On the other hand, you can get double quotes into a string like this: 

local Jacob a "puzzled" user? 

In this case, the delimiters are tacit. 

Similarly, double quotes within compound double quotes are not stripped:
as the c.d.q. are string delimiters, those are stripped. 

And so on. 

The key is that Stata will strip characters that are delimiters, but not
others. So, turn this round, and ask: What is acting as delimiters here?


Nick 
[email protected] 


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Jacob Wegelin
Sent: 08 January 2009 18:46
To: [email protected]
Subject: st: RE: display a local macro mixing text and numbers

Thanks to Nick Cox for his helpful and patient answer to my query.  But
I am a slow student:

(1) When does Stata interpret double quotes as literal double quotes, as
in the example provided by Nick Cox?

and

(2) When does Stata interpret double quotes as a sign that the
characters between the double quotes are to be interpreted as a string?

In the following example, the macro actually contains three blank
characters and contains no double quotes:

. local junk "   "

. di length("`junk'")
3

. di "`junk'"


.

Here is my proposed answer:

If double quotes appear in an unambiguous pair--that is, if the
"pairing" is unambiguous--then Stata interprets them as merely enclosing
a string. But if the double quotes are ambiguous, then Stata interprets
each double quote as a literal character to be included in the macro.

Is this correct?

Thanks for any enlightenment

Jacob Wegelin


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