Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Charalambos Karagiannakis <karagiannakis.charalambos@ucy.ac.cy> |
To | statalist@hsphsun2.harvard.edu |
Subject | RE: st: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command? |
Date | Mon, 22 Oct 2012 12:38:31 +0300 |
Dear Maarten and Nick, thank you very much for your help. As Mr. Nick described, the new variable I would like to create is not aligned with the original data. Off course I could have done it using vectors so that not to mess with the dataset, but for a couple of reasons I decided to do it this way. Charalampos K. -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Friday, October 19, 2012 2:29 PM To: statalist@hsphsun2.harvard.edu Subject: Re: st: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command? Maarten's bottom line is key. To expand on that, there are two over-arching points here: 1. For reduction to means of groups, -egen-'s -mean()- function with -by:- gets you there in one line. There is no need to write a loop. (Actually, you could do this without a loop even if -egen- didn't exist, as the code of -_gmean.ado- will show.) 2. Charalambos is writing the means in observations 1 up, so that they are not aligned with the original data. That's a kind of spreadsheet thinking, it seems to me. There may be a good reason for this, but for most purposes it's better to align group summaries with the original data, even though they are repeated for each observation in the group. -egen-'s -tag()- function offers an easy route to selecting one value from each group when that is what is wanted. Nick On Fri, Oct 19, 2012 at 8:34 AM, Maarten Buis <maartenlbuis@gmail.com> wrote: > On Fri, Oct 19, 2012 at 9:19 AM, Charalambos Karagiannakis wrote: >> sum time_quarter >> forvalues i = [r(min)](1)[r(max)] { > > The way to make that work is: > > forvalues i = `r(min)'(1)`r(max)' { > > or: > > forvalues i = `r(min)/`r(max)' { > > However, I suspect that you are much better of not running that loop > at all and instead look at the -by- prefix in combination with -egen-. > * * 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/