# RE: st: Aggregate constant to scalar and local

 From "Nick Cox" To Subject RE: st: Aggregate constant to scalar and local Date Fri, 13 Oct 2006 12:27:20 +0100

Uli is right. -_pctile- is faster than -summarize, detail-
to get the median. But, interestingly, not much: a few
quick experiments indicate ~10% faster. The story,
presumably, is that most of the work is in the sorting,
and the fact that -summarize, detail- calculates so much
more is secondary.

Nick
n.j.cox@durham.ac.uk

Ulrich Kohler

> Gawrich Stefan wrote:
> > I want to obtain aggregate values (e.g. median age by sex) from an
> > individual data set and generate locals out of them (to
> show the values in
> > a textbox in a graph).
>
> Others have proposed different solutions. Here is one that
> tries to be fast:
>
> . _pctile age if sex==1
> . local female_medage = r(r1)
> . _pctile age if sex==2
> . local female_medage = r(r1)
>
> or, slighly more general:
>
> . levelsof sex, local(K)
> . foreach k of local K {
> .	_pctile age if sex == `k'
> .	local medage`k' = `r1'
> . }
>
> (This produces local macros medage1 and medage2)
>
>
> The major lesson to be learned from this and the other
> solutions is to work
> with "saved results" (See -help return-, or chapter 4 in
> Kohler/Kreuter "Data
> Analysis Using Stata")

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/