Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command?

 From Nick Cox 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 Fri, 19 Oct 2012 12:28:43 +0100

```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/
```