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

st: RE: syntax; option real & varname


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: syntax; option real & varname
Date   Thu, 15 Aug 2002 09:46:50 -0400

> -----Original Message-----
> From: David Kantor [mailto:dkantor@jhu.edu] 
> Sent: Thursday, August 15, 2002 9:42 AM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: syntax; option real & varname
> 
> 
> Hi everyone.
> 
> I want to use -syntax- with an option, call it x1, which is 
> to be allowed 
> to be either a real number or a numeric variable.  Thus, I 
> want to combine 
> these two forms into one:
> 
>   syntax, x1(real)
> 
>   syntax, x1(varname numeric)
> 
> Is there an easy way to do this?  I already have a solution, 
> but I wonder 
> if someone can point out a better way.  
> 

I would get x1 as a string, and then test it after your syntax line:

   syntax , x1(string)

   capture confirm new variable `x1'
   if _rc {
      confirm number `x1'
      if _rc {
         di as error "x1() must be a new variable name or a number"
         error 198
      }
      else {
         local isvar 0
	}
   }
   else {
      local isvar 1
   }

This also sets the local macro isvar to indicate if x1 is a variable
name or a 
number, in case you need to take different action for each...  (Note, by
the way, that the text of your message suggests that you want an
existing varname; the syntax you give suggests a new varname.  If the
former, just take the "new" out of the "confirm new variable `x1'" line.

Nick Winter

> syntax newvarname (numeric) , x1(passthru)
> 
> local newvar "`varlist'"
> /* I captured `varlist', because I will now do several other -syntax- 
> commands (which will wipe out `varlist').
> */
> 
> local 0 ", `x1'"
> capture syntax , x1(real)
> if _rc {
>    syntax , x1(varname numeric)
> }
> 
> ////
> Thanks to anyone who has a better suggestion .
> -- David
> 
> David Kantor
> Institute for Policy Studies
> Johns Hopkins University
> dkantor@jhu.edu
> 410-516-5404
> 
> *
> *   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/
> 
*
*   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