Stata 15 help for quotes


[U] 18.3.1 Local macros and [U] 18.3.5 Double quotes


This help file documents the use of left and right single quotes in macros and the use of double quotes to enclose strings, in Stata commands, and in macros.


Remarks are presented under the following headings:

Single quotes Double quotes

Single quotes

An important note is on the way we use left and right single quotes within Stata, which you will especially deal with when working with macros (see [U] 18.3 Macros). Single quotes (and double quotes, for that matter) may look different on your keyboard, your monitor, and our printed documentation, making it difficult to determine which key to press on your keyboard to replicate what we have shown you.

For the left single quote, we use the grave accent, which occupies a key by itself on most computer keyboards. On U.S. keyboards, the grave accent is located at the top left, next to the numeral 1. On some non-U.S. keyboards, the grave accent is produced by a dead key. For example, pressing the grave accent dead key followed by the letter a would produce à; to get the grave accent by itself, you would press the grave accent dead key followed by a space. This accent mark appears in our help files as `.

For the right single quote, we use the standard single quote, or apostrophe. On U.S. keyboards, the single quote is located on the same key as the double quote, on the right side of the keyboard next to the Enter key.

Double quotes

Double quotes are used to enclose strings: "yes", "no", "my dir\my file", "`answ'" (meaning the contents of local macro answ, treated as a string), and so on. Double quotes are used by many Stata commands,

. regress lnwage age ed if sex=="female"

. gen outa = outcome if drug=="A"

. use "person file"

and double quotes are used with macros,

local a "example"

if "`answ'" == "yes" { ... }

In fact, Stata has two sets of double-quote characters, of which "" is one. The other is `""', and they work the same way as "":

. regress lnwage age ed if sex==`"female"'

. gen outa = outcome if drug==`"A"'

. use `"person file"'

No rational user would use `""' (called compound double quotes) instead of "" (called simple double quotes), but smart programmers do use them:

local a `"example"'

if `"`answ'"' == `"yes"' { ... }

Why is `"example"' better than "example", `"`answ'"' better than "`answ'", and `"yes"' better than "yes"? Only `"`answ'"' is better than "`answ'"; `"example"' and `"yes"' are no better - and no worse - than "example" and "yes".

`"`answ'"' is better than "`answ'" because the macro answ might itself contain (simple or compound) double quotes. The really great thing about compound double quotes is that they nest. Pretend that `answ' contained the string ``I "think" so''. Then,

Stata would find if "`answ'"=="yes" confusing because it would expand to if "I "think" so"=="yes"

Stata would not find if `"`answ'"'==`"yes"' confusing because it would expand to if `"I "think" so"'==`"yes"'

Open and close double quote in the simple form look the same; open quote is " and so is close quote. Open and close double quote in the compound form are distinguishable; open quote is `" and close quote is "', and so Stata can pair the close with the corresponding open double quote. `"I "think" so"' is easy for Stata to understand, whereas "I "think" so" is a hopeless mishmash. (If you disagree, consider what "A"B"C" might mean. Is it the quoted string A"B"C or is it quoted string A followed by B followed by quoted string C?)

Because Stata can distinguish open from close quotes, even nested compound double quotes are understandable: `"I `"think"' so"'. (What does "A"B"C" mean? Either it means `"A`"B"'C"' or it means `"A"'B`"C"'.)

Yes, compound double quotes make you think that your vision is stuttering, especially when combined with the macro substitution `' characters. That is why we rarely use them, even when writing programs. You do not have to use exclusively one or the other style of quotes. It is perfectly acceptable to code

local a "example"

if `"`answ'"' == "yes" { ... }

using compound double quotes where it might be necessary (`"`answ'"') and using simple double quotes in other places ("yes"). It is also acceptable to use simple double quotes around macros ("`answ'") if you are certain that the macros themselves do not contain double quotes or if you do not care what happens if they do.

Sometimes careful programmers should use compound double quotes, however. Stata's syntax command interprets standard Stata syntax, which makes it easy to write programs that understand things like

. myprog mpg weight if strpos(make,"VW")!=0

syntax works by placing the if exp typed by the user in the local macro if. Thus, `if' will contain ``if strpos(make,"VW")!=0'' here. Now, say that you are at a point in your program where you want to know whether the user specified an if exp. It would be natural to code

if `"`if'"' != "" { // the if exp was specified ... } else { // it was not ... }

We used compound double quotes around the macro `if'. The local macro `if' might contain double quotes, so we placed compound double quotes around it.


. webuse hbp2 . count if sex=="male" . label variable age_grp "age groups in 5-year increments"

. sysuse auto . char mpg[one] "this" . local x: char mpg[one] . display "`x'"

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