Statalist


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

st: RE: re: syntax


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: re: syntax
Date   Sun, 19 Oct 2008 16:26:26 +0100

(This posting underlines the value of asking people to refer to Stata
commands using the -cmdname- convention as the distinction between
syntax and -syntax- is crucial.) 

Macros 1, 2, etc. are nothing to do with the -syntax- command. The proof
of that is that they are defined regardless of whether you use -syntax-
to parse your syntax. 

At the start of a program or do-file macros 1, 2, etc. are the "tokens"
of your command line following your command name. What is a token? By
default, tokens are separated by spaces but bound by double quotes or
compound double quotes. In your examples, only spaces are of concern. 

In the first case, therefore, local macro 1 contains 

weight 

and local macro 2 contains 

weight2 

and the second case differs in that the local macro 2 contains 

weight2, 

as the comma is not treated as a separator in the default parsing. 

As you say, what you want will be obtained by inserting 

tokenize `varlist' 

after which local macros 1 and 2 will contain what you want. That is,
-tokenize- overrides the default tokenizing into arguments. 

The main point is covered in [U] 18.4 Program arguments. -tokenize- is a
little more advanced. Another way to do it is 

local 1 : word 1 of `varlist' 
local 2 : word 2 of `varlist' 

which is fine if it is clearer to you. Naturally you could use your own
names instead, say 

local y : word 1 of `varlist' 
local x : word 2 of `varlist'

although I suspect most Stata programmers would do the latter like this:


tokenize `varlist'
args y x 

To answer your question directly, -syntax- does not require white space
before or after the option comma. If it did, it would have rejected your
first example syntax as illegal. Rather, you need to keep in mind that
the numbered local macros 1 up are irrelevant to -syntax-. 

By the way, it is a definition of -syntax- that it operates on local
macro 0. Thus in some cases, -syntax- is used repeatedly in a program,
local macro 0 being defined upstream as needed. On occasion therefore
-syntax- can be used outside a program, but that's going beyond your
question. 

Nick 
n.j.cox@durham.ac.uk 
 
David Airey

This point is correct. Sorry about the other one. The question still  
remains.

I have not used syntax much. When I write the syntax command,

syntax varlist(min=2 max=2 numeric) [if] [in] [, noconstant]

I find it parses

mycommand weight weight2 , constant

differently than

mycommand weight weight2, constant

The top parses `2' to weight2 and the bottom parses `2' to weight2,  
(includes the comma). Use of tokenize varlist avoids this problem, but  
is it a long standing feature of syntax to require option commas to be  
separated by white space? I can't remember being careful about how I  
type that interactively.


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index