Statalist


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

AW: st: Should be simple yet ... how to write a function?


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: Should be simple yet ... how to write a function?
Date   Wed, 18 Feb 2009 11:44:51 +0100

<> 

" 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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Maarten buis
Gesendet: Mittwoch, 18. Februar 2009 11:39
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: Should be simple yet ... how to write a function?

--- On Wed, 18/2/09, Fabrice <fcaspam@yahoo.fr> 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/



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