[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: AW: can one program a function in Stata? |

Date |
Wed, 22 Apr 2009 18:25:31 +0200 |

<> Just define your very own -egen- function, taking a cue from the existing ones (they all start with an _g, so they are easy to locate on the ado-path). As it happens, Kit`s book has a section (3.4) on this subject... HTH Martin -----Ursprüngliche Nachricht----- Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Jacob Wegelin Gesendet: Mittwoch, 22. April 2009 18:22 An: statalist@hsphsun2.harvard.edu Betreff: st: can one program a function in Stata? 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 di r(myanswer) 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 ...? Would Kit Baum's book talk about this? Thanks for any insights Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin@vcu.edu URL: http://www.people.vcu.edu/~jwegelin * * 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/

**References**:**st: can one program a function in Stata?***From:*Jacob Wegelin <jacob.wegelin@gmail.com>

- Prev by Date:
**st: can one program a function in Stata?** - Next by Date:
**AW: st: How to change value based on string variable?** - Previous by thread:
**st: can one program a function in Stata?** - Next by thread:
**Re: st: can one program a function in Stata?** - Index(es):

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