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

RE: st: I'll never understand quotes


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: I'll never understand quotes
Date   Wed, 18 Jun 2003 11:07:27 +0100

Ernest Berkhout replied to Alan Feiveson: 

> >Hello - could someone please explain why statement #3 
> below fails if I put
> >double quotes around `sdir', but statement #2 needs to 
> have double quotes?
> >
> >1.      args gog sdir
> >2.      gen str5 sdir = "`sdir'"
> >3.      save gait_`gog'_`sdir', replace   (works)
> >3.      save gait_`gog'_"`sdir'", replace   (fails)
> 
> Lets take the danger of being only 99,9% correct and 
> explain the main 
> concept of it:
> 
> Your (single) macro quotes are needed to specify that 
> whatever is in the 
> local macro sdir is to be inserted at that point. If that 
> local macro would 
> contain the five characters abcde, things work out as follows.
> 
> In #2 you are specifiyng the *contents* of a new string  
> *variable*. That 
> always needs double quotes in normal syntax (I mean without 
> the local 
> macro's), like  in <gen str5 sdir="abcde"> . The command <gen str5 
> sdir=abcde> wouldn't work at the command line either.
> 
> In #4 you are specifiyng a part of a filename, which -apart 
> from the single 
> macroquotes- does not need any double quotes in normal 
> syntax, like in 
> <save filename_alan_abcde, replace>.
> At the commandline,  <save filename_alan_"abcde", replace> 
> would fail , and 
> that is exactly how Stata translates your code. It fails 
> because the 
> character " is not allowed in any filename.

My interpretation is slightly different. 

The use of macros is not an issue here, merely 
what Stata does with the line when they have been 
substituted, so that double quotes remain. 

It seems that the second double quote is being stripped 
by Stata as a delimiting quote, but the issue, as Ernest 
says, is then whether your (unstated) OS allows a 
double quote in a filename. 

However that stripping, if it's not an illusion, is 
a bit of a puzzle to me because Stata seems to prefer 
to strip delimiting quotes in pairs. 

On the more general issue of why we need double 
quotes, let me spell out the way that I understand it. 

You need double quotes to flag to Stata that an argument 
is a literal string. 

Hang on, you might say, isn't Stata (often) smart enough 
to figure that out for itself? After all

. gen str5 dir = 

should be enough of a hint that a string is 
coming. At least one reason for those quotes 
is that otherwise Stata has no way of distinguishing 
the string variable foo as argument from the literal string "foo". 
And in any case it helps the user keeps track, 
especially if you wanted to type 

gen str5 dir = ab c 

What is this? "ab c", you might say, but 
what about the leading space after 
the = and the trailing space you probably can't 
see after "c"? (I assure you that I typed one.) 
Being able to specify clearly "a b c" or "a b c " is 
evidently helpful from the user's point of view. 
So those two points alone are decisive, but not 
the whole story. 

So the standard argument is that you need double 
quotes to denote a literal string. They will then be 
stripped, unless in turn you use compound double 
quotes, but let's not get into that.... 

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