Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: forvalues loop doesn't work?


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: forvalues loop doesn't work?
Date   Mon, 7 May 2012 11:19:46 +0100

"loop" here is sloppy for innermost statements in the loop, namely the
-summarize- and -replace-.

Nick

On Mon, May 7, 2012 at 11:15 AM, Nick Cox <[email protected]> wrote:
> econqian222 is confusing the -if- command and the -if- qualifier. See
>
> FAQ     . . . . . . . . . . . . . . . . . . . . .  if command vs. if qualifier
>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  J. Wernow
>        6/00    I have an if command in my program that only seems
>                to evaluate the first observation, what's going on?
>                http://www.stata.com/support/faqs/lang/ifqualifier.html
>
> In brief, the command
>
> if index == `i'
>
> will always be interpreted as
>
> if index[1] == `i'
>
> which will be true just once. When that happens, the loop will be executed.
>
> Nick
>
> On Mon, May 7, 2012 at 10:52 AM, Ronnie Babigumira <[email protected]> wrote:
>
>> Looks like the problem is that the -replace- is not conditional so that last r(p90) replaces all
>>
>> Does this do what you want?
>>
>> qui sum index
>> forvalues i=1/`r(max)'{
>> sum quantity if index == `i', detail
>> replace q_cutoff90=r(p90) if index == `i'
>> }
>
>
> On Monday, May 7, 2012 at 11:42 AM, econqian222 wrote:
>
>>> I have tried to record percentile 90 of quantity for each value of
>>> index, unfortunately, my result still only record the percentile 90 of
>>> quantity for all the values of index, so what's the problem of my program?
>>> My program goes like the following:
>>>
>>> forvalues i=1/4760{
>>> if index==`i' {
>>> sum quantity, detail
>>> replace q_cutoff90=r(p90)
>>> }
>>> }
>>>
>>> My data goes as this:
>>> index quantity q_cutoff90
>>> 1 4 .
>>> 1 67 .
>>> 2 89 .
>>> 2 57 .
>>> 2 91 .
>>> 3 98 .
>>> 3 95 .
>>> 3 27 .
>>> . . .
>>> . . .
>>> 4760 88 .
>>> 4760 56 .
>>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index