Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Confusion about using Stata local variables in Mata, the -st_local- function and the -stata- command |
Date | Sun, 10 Nov 2013 14:00:11 +0000 |
You can do this easily stata("save Temp\temp" + strofreal(i) + ".dta") You are just handling to Mata a string expression, which will be evaluated before it is passed to the Mata function -stata()-. Absolutely no need to reach for a local macro. For more, see e.g. http://www.stata-journal.com/article.html?article=pr0052 Nick njcoxstata@gmail.com On 10 November 2013 13:32, Milen Stoyanov <milen_stoyanov@gbg.bg> wrote: > I have a dataset that I have created in Mata and then exported to Stata. I am creating the dataset in a loop, so that on each iteration the dataset is different. I would like to save each of those datasets, so what I am trying to do is the following: > > mata > for (i=1;i<=d;i++) { > "generate dataset" > "export to Stata" > ..... > st_local("counter", strofreal(i)) > stata(`"save "Temp\temp_`counter'.dta", replace"') > } > end > > I am having no troubles with my first 2 steps, which are "generate dataset" and "export to Stata", that is why I am only indicating that there is such kind of code, but it is long and I don't believe it is important for this particular question of mine. > > So I am trying to save a new dataset of the form "temp_`counter'.dta" each time, where counter is going from 1 to about a 1000. In the end I was hoping to get files "temp_1.dta", "temp_2.dta", ... "temp_1000.dta" in my folder. I think that the line "st_local("counter", strofreal(i))" works fine and it defines the local variable correctly, but then the save command in stata does not work as intended by me. So what happens in the end is that the loop does not break or give an error message, but each single time it saves over the file "temp_.dta", so it ignores the counter whatsoever. > > Does anyone has an idea how I might make this work? Thanks for the help in advance. > > > * > * 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/ * * 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/