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

Re: st: .ado file questions


From   David Kantor <dkantor@jhu.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: .ado file questions
Date   Wed, 18 May 2005 15:30:46 -0400

At 11:16 AM 5/18/2005 -0700, Florence wrote:
Hello everyone, I am writing a .ado file that is supposed to mimic what
would be done by a "function" in other languages.  The problems I'm
running into are:

[...]

(2) Is there a way to FORCE stata to return a function after the .ado file
completes?  So far, I have just been creating scalars and matrices inside
of the .ado file, but I think this is probably not that efficient for my
purposes since I really want the function ITSELF to take on a value or a
matrix.

[...]

THANKS SO MUCH!!!
Florence
You've already gotten a response from Nick Cox. I'd like to add to this regarding (2).

In Stata, you cannot define a function in the usual sense, as the term is used in general programming languages. That is, you cannot create something that can be invoked in an expressions context -- and which yields a value which is substituted for the function name in that context. Stata has plenty of these, but they are all built-in. (E.g., ln(). )

But what you can do is create a program that computes something and leaves the results somewhere for you to pick up later. This can be a variable, matrix, scalar, global macro, or a returned value. (Also, it can be a local macro, but that is rarely done, is not recommended, and requires an undocumented feature. So I won't tell you how.)

I suggest you learn about returned values. See -help return- and -help program-. In the latter, pay attention to the rclass, eclass, and sclass options. You probably want rclass to start with.

I would divide programs into two categories: those that are made for a specific situation, and those that are general. The former may reference and create specifically-named objects. The latter should not; I would include any ado program in this category.

If your program is to leave something in a variable, and this is to be a generalized purpose program, then your variable can have its name passed in via an option. Typically this is done as in...
myprog .... , gen(newvar)

(Within the program, you would have
syntax ... , gen(name)
)
The program then deposits the computed values into `gen'. The calling context, after the call, knows it as newvar.

And other to-be-created objects can be passed in that way: matrices, etc.

But a good way to pass a matrix back is to use -return matrix ...-. Even if the matrix had a tempname in within the program, it still stays around, under the returned name.

I hope this helps.
-- 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/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index