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

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

From   "Fabrice" <>
To   <>
Subject   st: RE: Should be simple yet ... how to write a function?
Date   Wed, 18 Feb 2009 18:50:50 +0100

Thanks to all of you for your thoughtful answers! 

I've learned much, including--but not restricted to--...
a) that most experts agree that Stata indeed does not implement the most
basic form of programming (function passing direct return values)
b) that Stata has a wonderful programming language with Mata (I had read
about it, but it's impressive what is a priori possible there)
c) that Stata has an object oriented engine (hidden behind the class
structure), whoa!

What is still unclear to me is the following: Maarten suggested that I could
make function returning variables, like many egen function. And the trick is
simply to look into the .ado code of those _gxxx.ado functions. After
looking briefly at such code, I'm still not clear about the details, but it
sounds like a good idea.

Then I noticed that some simple scalar function (such as acos()) appear also
there in the library provided by Stata, and wondered: hey, why not just
imitate what's there?!

Currently, the only example I can find are Mata code. 

So my remaining question is therefore: I know I can write 
. scalar y = sin(x)

So .... where is the code for such "sin" function. I tried ". which sin",
and it responds that it is not a built in or ado-file. So where is it

Maybe that is all implemented in C code in the kernel, and then, I'm toast.
But it does not hurt to ask...

Best to all

Fabrice Cavarretta
Assistant Professor of Management and Entrepreneurship
ESSEC Business School

-----Original Message-----
[] On Behalf Of Martin Weiss
Sent: mercredi 18 février 2009 11:10
Subject: st: AW: Should be simple yet ... how to write a function?



When you -generate- a variable, you implicitly apply a "function" to all
observations. If you want to write your own function, -egen- is a good
starting point, and this is explored in the article cited above...


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Fabrice
Gesendet: Mittwoch, 18. Februar 2009 11:04
Betreff: st: Should be simple yet ... how to write a function?

Im a pretty advanced Stata user, but have a simple question I cant figure
out: 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

This seems it should be  available somewhere in the language, yet I?ve
searched in a lot of programming explanations about Stata and never found a
way to write that.  FYI, I program extensively in stata, know about r-type
vs. e-type program, and know how to store results to return to main program.

I just don?t know how to write the most simple function form.

Any idea?


*   For searches and help try:

*   For searches and help try:

*   For searches and help try:

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