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: How to insert scalars (elements from Stata's matricies) as locals' values in the 'forvalues' command?


From   Nick Cox <njcoxstata@gmail.com>
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 14:40:37 +0100

I can't comment on reasons you don't give us, but this is dangerous
practice. You have only to sort the data even slightly and you've lost
track of which mean came from where. In fact you might not even notice
that that had happened.

Nick

On Mon, Oct 22, 2012 at 10:38 AM, Charalambos Karagiannakis
<karagiannakis.charalambos@ucy.ac.cy> wrote:
> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index