Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: RE: getting functions / commands to return multiple varibles


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: getting functions / commands to return multiple varibles
Date   Fri, 20 Jul 2012 17:04:19 +0100

This isn't hard and it's been a standard desire through most of Stata's history. 

In a command, you can just specify (e.g.) a -generate()- option that takes a newvarlist. 

In a Mata function, you can do the same thing. You pass new names to a Mata function and pass results back to Stata as variables. 

You need not even know in advance how many variables will be created, although your code will need to work that out. 

Look (e.g.) at the source code of -separate- or -split- (official commands) or -rowsort-, -rowranks- (SJ). These are just arbitrary examples: the technique is widely used. 

But the syntax is typically nothing like your example. Your example is programmable, but it is easier to pass new names via a program option. 

All that said, if I wanted to do your examples, I would definitely not write a new program. I would just loop over the possibilities. 

Nick 
n.j.cox@durham.ac.uk 

Pradipto Banerjee

In a lot of my codes, I tend to do multiple calls that are quite similar, for example:

. by var1 var2: egen varl=pctile(var3), p(20) . by var1 var2: egen varh=pctile(var3), p(80)

I was wondering if anyone has had experience designing a function that can return two variables, say for example:

. by var1 var2: myegen {varl, varh} = mypctile(var3), p({20,80}) . by var1 var2: myegen {varl, varm, varh} = mypctile(var3), p({20,50,80})

Is this a really hard problem?

Have others worked on this before? If so, would it be possible to share any links, websites, etc.

*
*   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   |   Site index