Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: getting functions / commands to return multiple varibles
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
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
[email protected]
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/