⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quotes.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
字号:
{smcl}
{* 05apr2005}{...}
{cmd:help quotes}
{hline}

{title:Title}

{pstd}
{hi:[U] 18.3.5 Double quotes}


{title:Remarks}

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

	    . {cmd:regress lnwage age ed if sex=="female"}

	    . {cmd:gen outa = outcome if drug=="A"}

	    . {cmd:use "person file"}

{pstd}
and double quotes are used with macros,

	    {cmd:local a "example"}

	    {cmd:if "`answ'" == "yes" {c -(}}
		    ...
	    {cmd:{c )-}}

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

	    . {cmd:regress lnwage age ed if sex==`"female"'}

	    . {cmd:gen outa = outcome if drug==`"A"'}

	    . {cmd:use `"person file"'}

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

	    {cmd:local a `"example"'}

	    {cmd:if `"`answ'"' == `"yes"' {c -(}}
		    ...
	    {cmd:{c )-}}

{pstd}
Why is {cmd:`"example"'} better than {cmd:"example"}, {cmd:`"`answ'"'} better
than {cmd:`"answ"'}, and {cmd:`"yes"'} better than {cmd:"yes"}?  The answer is
that only {cmd:`"`answ'"'} is better than {cmd:"`answ'"}; {cmd:`"example"'}
and {cmd:`"yes"'} are no better - and no worse - than {cmd:"example"} and
{cmd:"yes"}.

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

    Stata would find{col 45}{cmd:if "`answ'"=="yes"}
    confusing because it would expand to{col 45}{cmd:if "I "think" so"=="yes"}

    Stata would not find{col 45}{cmd:if `"`answ'"'==`"yes"'}
    confusing because it would expand to{col 45}{cmd:if `"I "think" so"'==`"yes"'}

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

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

{pstd}
Yes, compound double quotes make you think your vision is stuttering,
especially when combined with the macro substitution {cmd:`'} 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

	    {cmd:local a "example"}

	    {cmd:if `"`answ'"' == "yes" {c -(}}
		    ...
	    {cmd:{c )-}}

{pstd}
using compound double quotes where it might be necessary ({cmd:`"`answ'"'})
and using simple double quotes in other places (such as {cmd:"yes"}).  It is
also acceptable to use simple double quotes around macros (e.g.,
{cmd:"`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.

{pstd}
There are instances where careful programmers should use compound double
quotes, however.  Stata's {helpb syntax} command interprets standard Stata
syntax, and so makes it easy to write programs that understand things like

	    . {cmd:myprog mpg weight if index(make,"VW")!=0}

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

	    {cmd:if `"`if'"' != "" {c -(}}
		    {cmd://} {it:the if exp was specified}
		    ...
	    {cmd:{c )-}}
	    {cmd:else {c -(}}
		    {cmd://} {it:it was not}
		    ...
	    {cmd:{c )-}}

{pstd}
Note that we used compound double quotes around the macro {cmd:`if'}.
The local macro {cmd:`if'} might contain double quotes, so we placed
compound double quotes around it.


{title:Also see}

{psee}
Manual:  {bf:[U] 18.3 Macros},{break}
{bf:[U] 18.3.1 Local macros},{break}
{bf:[U] 18.3.5 Double quotes}

{psee}
Online:  {helpb macro}; {help language}; {help exp}
{p_end}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -