Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: forvalues loop doesn't work? |
Date | Mon, 7 May 2012 11:15:07 +0100 |
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 <rb.glists@gmail.com> 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/