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

Re: st: Stata 6 vs. 7


From   Weihua Guan <wguan@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Stata 6 vs. 7
Date   Mon, 09 Sep 2002 12:30:16 -0500

--Hung-Jen Wang <hjwang@econ.sinica.edu.tw> wrote:                    

[...]
> Simply put, my problem is this: A (empty or non-empty) varlist needs to be
> specified by an -option-, and so the -option- should be able to
> accommodate both empty and non-empty varlist (for Stata 6).

Hung-Jen met the problem when he defined the syntax of a program as:

	program define myexpl
		version 6.0
		syntax ...., ..... [myopt(varlist) .... ]

Stata 6 does not allow an empty varlist, so when the user tries

. myexpl, myopt()

Stata will complain that "myopt() invalid" because the option -myopt()- does
not contain a valid varlist.  

To work around the problem, Hung-Jen may try -myopt(passthru)-.  See the
following example:

	program define myexpl
		version 6
		syntax [, myopt(passthru)]
		if "`myopt'" != "" {
			di "myopt is: `myopt'"
			tokenize `myopt', parse("()")
			local myopt `3'
			summ `myopt'
		}
		else {
			di "myopt() is empty"
		}
	end

-passthru- tells Stata to pass anything specified in the option into the
program, including nothing.  Then the empty varlist will be allowed 

. myexpl, myopt()
myopt() is empty

Here the local macro `myopt' is empty when we specify nothing in the
corresponding option.  However, when -myopt()- is not empty, the local macro
`myopt' will contain the full name of the option, i.e,. "myopt(varlist)".  We
then need to use -tokenize- to extract the real variable list out from this
string.  For example,

. myexpl, myopt(price mpg)
myopt is: myopt(price mpg)	<----- note what `myopt' contains 

Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
   price |      74    6165.257   2949.496       3291      15906  
     mpg |      74     21.2973   5.785503         12         41  


In fact, specifying a empty varlist within -myopt()- is equivalent to not
specifying this option at all:

. myexpl
myopt() is empty


Weihua Guan <wguan@stata.com>
Stata Corp.

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