Re: st: can one program a function in Stata?

 From "Martin Weiss" To Subject Re: st: can one program a function in Stata? Date Wed, 22 Apr 2009 18:57:44 +0200

```<>

```
We can also get a grip on the prob using -cond()- so no heavy machinery required...
```

****
clear*
set obs 10

gen x=_n
gen myanswer=cond(x>3,(sqrt(x) + 5)^(2/3), (sqrt(x) + 9)^(2/3))

list, noobs
****

HTH
Martin
_______________________
```
----- Original Message ----- From: "David Kantor" <kantor.d@att.net>
```To: <statalist@hsphsun2.harvard.edu>
Sent: Wednesday, April 22, 2009 6:45 PM
Subject: Re: st: can one program a function in Stata?

```
```At 12:21 PM 4/22/2009, Jacob wrote:
```
```I am about to order Kit Baum's Introduction to Stata Programming. But
I'm curious whether his book will answer the following question.

```
Stata has what I would call plain old functions, of which sqrt() is an example:
```
. di sqrt(3.1415926535)
1.7724539

. sysuse auto, clear
(1978 Automobile Data)

. gen sqrtTurn=sqrt(turn)

But suppose I want to define a function that's not found under -help
functions-. The following approach (with a hypothetical nonstandard
function) seems clumsy:

capture program drop myownfunction
program define myownfunction, rclass
if `1' > 3 {
return scalar myanswer= (sqrt(`1') + 5)^(2/3)
}
else {
return scalar myanswer= (sqrt(`1') + 9)^(2/3)
}
end

myownfunction 3.1415926535

Besides the awkward business of having to first call the function,
then refer to - r(myanswer) - , this program only works for a single
number (a scalar in the mathematical not Stata sense). If I want to
apply it to an entire column of numbers (a Stata numeric variable),
I'd have to write a different function, then always remember whether
to call the scalar or the vector (variable) version. On the other
hand, we can use sqrt() directly for either purpose.

Thus, is there a way to define -myownfunction()- so that one can call
it just as one calls sqrt()?  Does one do this with -mata-, or ...?
```
```
```
This gets asked fairly often. To my knowledge, there is no way to do what you asked for. (I don't know enough about mata to comment about that.)
```
```
Stata doesn't let you do that, but you can have programs that deposit a value somewhere -- a variable, a scalar, an r() value (as you showed in your example), a global macro, a local macro (though there are issues about that, and it is discouraged). Despite this limitation, Stata users manage fairly well.
```
```
A notable example is -egen-, which is really a front end for a large suite of programs that deposit values into variables. -egen- is extensible; you can add your own programs to it. We often refer to these programs as "functions", though they really aren't functions. They just appear in a context and syntax that makes them look like functions. To see how to write an egen "function" see...
```viewsource egen.ado
plus, look at one of the Stata-given programs, such as max (_gmax):

HTH
--David

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```