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: mata in a while loop


From   "Ozimek, Adam" <Ozimek@econsult.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   RE: st: mata in a while loop
Date   Sat, 8 Oct 2011 12:49:20 -0400

Nick,

Thanks, I've got it working now. And I did see the other comments of yours, they were very helpful. Thanks again.  

Adam 


________________________________________
From: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox [njcoxstata@gmail.com]
Sent: Friday, October 07, 2011 6:33 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: mata in a while loop

I wasn't aware that was legal code.

This worked for me

local test = 0
file open finished using outputtest.txt, write text replace

while `test' <10 {
          mata :  fields = tokens(st_local("test"))
          mata :  st_local("output",fields[1])
        file write finished "`output'" _n
        local ++test
}
file close finished

Nick

P.S. note also my three postings in reply to yours of
http://www.stata.com/statalist/archive/2011-09/msg00973.html

On Fri, Oct 7, 2011 at 11:14 PM, Ozimek, Adam <Ozimek@econsult.com> wrote:

> I'm working on a program and I'm having a problem with the part of it where I do some part of it in mata within a while loop in stata. Here is a very simple mockup of what I'm trying to do:
>
>
> local test = 0
> file open finished using outputtest.txt, write text replace
>
>      while `test' <10 {
>            mata {
>                        fields = tokens(st_local("test"))
>                        st_local("output",fields[1])
>            }
>
>        file write finished "`output'" _n
>        local ++test
>     }
>
>
> The while loop runs fine 9 times but then when `test' = 10, it gives me this error:
> "unrecognized command:  } invalid command name"
>
> and my output file looks like this:
>
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 9
>
> What am I doing wrong? ( Please ignore that it is a senseless program in itself, this mockup is broken in the same manner my real program is broken, so if I understand this hopefully I'll understand my full program problem.)

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