Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Francis, Richard N" <rnfrancis@utep.edu> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: Retrieving data from matrix |
Date | Mon, 2 Dec 2013 22:48:15 +0000 |
Nick, If the code below is creating new columns for each pass through the loop, how would I modify the code to create new ROWS instead? destring gvkey, replace egen id = group(gvkey), label gen date1 = yq(year, fqtr) tsset id date1, quarterly su id, meanonly local limit=r(max) foreach v in k maic sic rmse DFGLS { g `v' = . } gen which = "" forval i = 1/`limit' { display "Iteration of `i' of `limit" replace which = "`: label (id) `i''" in `i' dfgls fcf if id == `i' , maxlag(4) ers matrix results=r(results) svmat results, names(iter`i'_) qui foreach v in k maic sic rmse DFGLS { replace `v' = r(`v') in `i' } } Thank you for all you do! Rick Francis -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Monday, December 02, 2013 3:01 PM To: statalist@hsphsun2.harvard.edu Subject: Re: st: Retrieving data from matrix The error is coming from -svmat-. Consider that _second_ time around the loop -svmat- is trying to use the names for new variables that it used _first_ time around the loop. You must exploit the -name()- option to use different names each time around the loop. e.g. svmat results, names(iter`i'_) Nick njcoxstata@gmail.com On 2 December 2013 21:22, Francis, Richard N <rnfrancis@utep.edu> wrote: > Hi Statalist members, > > Have a simple program below, but receive the following error: > > "new variables cannot be uniquely named or already defined" > > I am using Stata13 with Windows. > > The first execution of the loop works fine, then as the second loop is ending, I receive the "new variables cannot be uniquely named or already defined" error message. > > Here is the code: > > destring gvkey, replace > egen id = group(gvkey), label > gen date1 = yq(year, fqtr) > tsset id date1, quarterly > su id, meanonly > local limit=r(max) > foreach v in k maic sic rmse DFGLS { > g `v' = . > } > gen which = "" > > forval i = 1/`limit' { > display "Iteration of `i' of `limit" > replace which = "`: label (id) `i''" in `i' > dfgls fcf if id == `i' , maxlag(4) ers > matrix results=r(results) > svmat results > qui foreach v in k maic sic rmse DFGLS { > replace `v' = r(`v') in `i' > > } > } > > > In this case, the matrix is 4x5, and the replace `v' command above creates 4 new rows. > > How would I modify this to create only one new row from the matrix? > > Any ideas are greatly appreciated! > > Thank you for your time and expertise! > > > Rick Francis > > > * > * 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/ * * 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/