Re: st: Errors "out of range" for a simple Mata function

That's a helpful update on whether the functions -tokens()- [NB] is needed.
I won't deny myself new features of Stata when they are needed, but I
am happy to be slightly out-of-date in writing whatever I put into the
public domain, so that (in this case) anyone still on Stata 9 or Stata
10 can use a program too -- or even a code fragment.
Christophe's allusion is to -help whatsnew10to11-.
Nick
On Wed, Nov 21, 2012 at 8:17 AM, Christophe Kolodziejczyk
<ck.statalist@gmail.com> wrote:
> Just a minor point on the use of the token() function together with
> st_data() and st_view() (or even st_store()).
> Token() creates a vector which elements contain the different words of
> a string and is no longer necessary since Stata 11. It may be
> necessary with st_sdata() and st_sview() with Stata 11 though. You can
> write
>
> st_view(X = .,.,varlist) instead.
>
> See help whatsnew for more details
>
> Christophe
>
>
> 2012/11/20 Nick Cox <njcoxstata@gmail.com>:
>> Thanks for the correction. The word "wrong" was itself wrong. I was
>> elevating my personal preference only to use views for data Ithat I
>> won't change to an implication that you can't do it, which was
>> incorrect.
>>
>> Given
>>
>> local macro generate
>> local macro varlist
>> local macro touse
>>
>> as discussed to date in the thread
>>
>> . mata : st_store(., "`generate'", "``touse''", rowsum(st_data(.,
>> tokens("`varlist'"), "``touse''")))
>>
>> is I think a complete solution. However, I'd sympathise with anyone
>> who thought that overvalues conciseness. Also, using Mata as a
>> calculator in this way undermines Ly Tran's major purpose of using the
>> row sum problem as a vehicle for learning Mata.
>>
>> Nick
>>
>> On Tue, Nov 20, 2012 at 2:01 AM, Christopher Baum <kit.baum@bc.edu> wrote:
>>
>>> Ly Tran:
>>>
>>> Nick Cox is correct in pointing out that you need to pass `generate' to your Mata function. But it is not necessary to use st_store() as he suggests. Your Mata code works with one small change:
>>>
>>> capture program drop testsum
>>> program testsum, rclass
>>> syntax varlist(numeric) [if] [in], GENerate(string) [FLOAT]
>>> qui generate `float' `generate' = .
>>> mata: tsum( "`varlist'", "`generate'")
>>> end
>>>
>>> mata
>>> void function tsum (string scalar varlist, string scalar newvar)
>>> {
>>> real matrix X, Z
>>> st_view(X = .,., tokens(varlist))
>>> st_view(Z =., ., newvar)
>>> Z[.,.] = rowsum(X)
>>> }
>>> end
>>>
>>> When an entire view matrix is the LHS of a statement, you must use the syntax shown, rather than the name of the view matrix, for the statement to update the view matrix (and the Stata variable(s)) and not create a new Mata object. In the help for st_view(), read the section "Cautions when using views 2: Assignment" to see this point illustrated. It is also mentioned in the Mata chapter of ITSP below.
>>>
>>> . sysuse auto,clear
>>> (1978 Automobile Data)
>>>
>>> . testsum price mpg turn, gen(junk)
>>>
>>> . egen junk2=rowtotal(price mpg turn)
>>>
>>> . su junk*
>>>
>>> Variable | Obs Mean Std. Dev. Min Max
>>> -------------+--------------------------------------------------------
>>> junk | 74 6226.203 2948.149 3354 15972
>>> junk2 | 74 6226.203 2948.149 3354 15972
>>>
>>>
>>> One other thing: if you allow [if] and [in] in your ado-file, you should -marksample touse- and pass `touse' to Mata so that it can limit the observations being used in the Mata routine. Again, example code in ITSP.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/