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

RE: st: RE: programs with options


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: programs with options
Date   Fri, 11 Jul 2003 16:25:28 +0100

Jean-Benoit Hardouin

> See for example
>
>
> .testop var1 var2 ,option1
> var1
> var2
>
> .testop var1 var2, option1
> var1
> var2,
>
> .testop var1 var2,option1
> var1
> var2,option1
>
> How can I use the name of the second variable in my program
> without the
> comma if this one is not separated by a blank of the last variable ?

As hypothesised, this isn't anything to do with -syntax-.

What you type after the command name is automatically
parsed by Stata into "words", parsing on spaces, so that
in your case the first word after the command name in the last
example is

var1

and this is put into local macro 1, and the second
word is

var2,

and this is put into local macro 2.

The fix you need is

tokenize `varlist'

to overwrite the default assignment to locals
1, 2, etc.

Your program would then be

program define testop
	version 7
	syntax varlist(min=2 numeric) [, option1 option2]
	tokenize `varlist'
	local nbitems : word count `varlist'

	forvalues i = 1/`nbitems' {
		di "``i''"
	}
end

but note that there is a better way,
even in Stata 7,

program define testop
	version 7
	syntax varlist(min=2 numeric) [, option1 option2]

	foreach v of local varlist {
		di "`v'"
	}
end

However, no doubt you constructed the program just
to demonstrate the problem.

The main point: use -tokenize `varlist'- to overwrite
the default assignment to local macros 1 up.

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