Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: MATA: Dynamic Variable Names in MATA


From   "Sergiy Radyakin" <serjradyakin@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: MATA: Dynamic Variable Names in MATA
Date   Sat, 22 Mar 2008 18:42:13 -0400

Dear Nicholas,

the feature that would be helpful in your case is called a matrix. In
fact MATA is very good at manipulating matrices.

mata
hello=J(1,10,.)
for(x=1;x<=count; x++) {
  hello[x]=x*10
}
hello[3]

The way of manipulating variables you described was devised to
overcome the handicap Stata had at that time (no arrays). Since it
supports arrays now, there is really no need to use this (IMHO exotic)
way of programming.

Best regards, Sergiy Radyakin



On 3/22/08, Nicholas Harrigan <nicholas.harrigan@nuffield.ox.ac.uk> wrote:
> Hi,
>
> Is there anyway of dynamically generating variable names in MATA?
>
> For example, I would like to be able to write something like this:
>
> count = 10
> for(x=1; x<=count; x++) {
> y = strofreal(x)
> hello_`y' = x*10
> }
>
> and is this way generate 10 variables named hello_1, hello_2, ... hello_10.
>
> I know that the STATA local ` ' syntax doesn't work in MATA, but it
> would be very very helpful to me to be able to be able to dynamically
> generate variables in MATA.
>
> If there isn't anyway of dynamically generating variables in MATA, are
> there any work arounds you suggest?
>
> Thanks and best wishes
> Nick
>
> --
> Nicholas Harrigan
> Post-doctoral research officer
> Nuffield College, Oxford, OX1 1NF
> Ph: +44 (0) 18652 78548
> M: +44 (0) 7962 627960
> nicholas.harrigan@nuffield.ox.ac.uk
>
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index