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

st: RE: Programming question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Programming question
Date   Wed, 26 Jun 2002 14:57:38 +0100

S Mahmud
>
> If you write:
>
> program define myprog
>    version 7
>    syntax [varlist]
>
>
> Stata validates the list of the variable names passed by the user and
> complains if a variable does not exist e.g.
>
> . myprog  unknownvar
> variable unknownvar not found
> r(111);
>
>
> However, the insheet internal command apparently allows the user
> to enter a
> list of names for non-existing variables without a problem. Is it possible
> to do the same thing from an ado command? And how?

You can do this in several ways.

In general, you are not obliged to use -syntax-
in a program. You could use -gettoken- or -args-
or some other method to work with your input
or indeed rely on Stata's default parsing.

For example with

program def myprog
	di "first argument is `1'"
	di "second argument is `2'"
end

and

myprog unknownvar

the first argument supplied will be -display-ed
and _could_ be used in some way. Here the program (and indeed Stata) neither
knows nor cares what "unknownvar" is: it is just a character string.

With -syntax- you could use

syntax anything

or

syntax newvarlist

or

syntax newvarname

With the latter two, you are _not_ obliged to create a new variable.
All that happens is frontier control: Stata affirms that what is
presented as a newvarname when you enter the program is not, at
that point, the name of an existing variable and would be a legal
name for a new variable. What you do thereafter is up to you.

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