<>
" but that will only function as a function within Mata."
Note, though, that you can switch between Mata and Stata quite easily as in - help m4_stata- so this need not be a severe limitation of your opportunities...
See also http://www.stata.com/meeting/fnasug08/baum_StataMata.beamer.FNASUG08.pdf
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im Auftrag von Maarten buis
Gesendet: Mittwoch, 18. Februar 2009 11:39
An: [email protected]
Betreff: Re: st: Should be simple yet ... how to write a function?
--- On Wed, 18/2/09, Fabrice <[email protected]> wrote:
> how does one write a *function*, i.e. a program that returns
> a value, and that can be called like local y = fct(x), where
> x and y are numbers or strings. <snip>
> FYI, I know about r-type vs. e-type program, and know how to
> store results to return to main program.
Short answer: you can't.
Longer answer:
You can leave a local behind using the not documented
-c_local- command, so in the example below the program
square leaves behind a local called foo
*------------ begin example ---------
capture program drop square
program define square
c_local `1' = `2'^2
end
square foo 2
di `foo'
*------------ end example -----------
You can make a function like program that works with
the -egen- command, but this will only make variables.
To do so call your program _g<whatever>.ado, to create
the whatever "function". You can look at
-viewsource _gcount.ado- to see an example of such a
program. Alternatively you can make a function in Mata,
but that will only function as a function within Mata.
Hope this helps,
Maarten
-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands
visiting address:
Buitenveldertselaan 3 (Metropolitan), room N515
+31 20 5986715
http://home.fsw.vu.nl/m.buis/
-----------------------------------------
*
* 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/