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 at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st: beginer's mata function in dofile


From   tashi lama <ltashi32@hotmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: beginer's mata function in dofile
Date   Tue, 7 Aug 2012 15:00:57 +0000

thanx although I think it should be 
b=(1::rows(Y)) instead of b=(1..rows(Y))

----------------------------------------
> From: kit.baum@bc.edu
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: beginer's mata function in dofile
> Date: Sat, 4 Aug 2012 14:01:08 +0000
>
> <>
> On Aug 4, 2012, at 2:33 AM, Tashi wrote:
>
>> I am trying to learn Mata and this is my first do file with mata function. I mimicked the code originally written by William Gould of StataCorp titled "Mata Matters: Creating new variables-sounds boring, isn't".
>>
>> bp1 bp2 bp3 bp4 bp5 |
>> |-----------------------------|
>> | 170 168 166 163 161 |
>> | 158 156 154 151 147 |
>> | 161 158 155 151 147 |
>> | 155 153 151 148 144 |
>> | 158 160 160 161 162 |
>>
>> begin do-file mata1.do
>> capture program drop nn
>> mata: do_trend_var("b", "bp1 bp2 bp3 bp4 bp5")
>> list
>> end
>> function do_trend_var(string scalar newvarname, string scalar varnames)
>> {
>> real matrix Y
>> real colvector b
>> real scalar idx
>> st_view(Y, ., varnames)
>> b =1\2\3\4\5
>> idx = st_addvar("float", newvarname)
>> st_store(., idx, b)
>> }
>> exit
>> nn
>>
>> when I run, stata isn't happy and throws back
>> <istmt>: 3499 do_trend_var() not found
>> r(3499);
>>
>> Any idea?
>
> (1) You are not defining the program nn.
> (2) You are not going into Mata before defining the function.
> (3) You are not exiting Mata with an -end-, which is required.
>
> The following works. Examples of these constructs are in -ISP- below; the code files are freely downloadable from stata-press.com.
>
> // begin do-file mata1.do
> clear all
> set obs 10
> forv i=1/5 {
> g bp`i' = runiform()
> }
> capture program drop nn
> prog nn, rclass
> mata: do_trend_var("b", "bp1 bp2 bp3 bp4 bp5")
> list
> end
> version 12
> mata:
> void function do_trend_var(string scalar newvarname, string scalar varnames)
> {
> real matrix Y
> real colvector b
> real scalar idx
> st_view(Y, ., varnames)
> b = (1..rows(Y))'
> idx = st_addvar("float", newvarname)
> st_store(., idx, b)
> }
> end
> nn
>
> Kit
>
>
> Kit Baum | Boston College Economics & DIW Berlin | http://ideas.repec.org/e/pba1.html
> An Introduction to Stata Programming | http://www.stata-press.com/books/isp.html
> An Introduction to Modern Econometrics Using Stata | http://www.stata-press.com/books/imeus.html
>
>
> *
> * 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   |   Site index