# Re: st: Aggregate constant to scalar and local

 From "jean ries" To statalist@hsphsun2.harvard.edu Subject Re: st: Aggregate constant to scalar and local Date Fri, 13 Oct 2006 12:29:07 +0200

```I think you don't need egen and sorting. Just try this:

sysuse auto, clear
summarize price if foreign == 0, detail
local mp0 = r(p50)
summarize price if foreign == 1, detail
local mp1 = r(p50)

On 10/13/06, Gawrich Stefan <s.gawrich@hlpug.hessen.de> wrote:
```
```Hi statalisters,

maybe a silly question but is there a easier and more efficient solution to
I didn't manage to find one even after some time of research...

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).

My solution: Calculate the sex-specific median via "egen" and afterwards
sort the data ascending and descending by sex, each time copying the first
medage observation to a scalar.
bysort sex: egen medage = median(age)
gsort sex
scalar male_medage = medage
gsort -sex
scalar female_medage = medage
local male_medage = male_medage
local female_medage = female_medage

This seems not to be very smart because two times sorting is inefficient and
taking data from the first observation in this way easily lead to errors (if
there are changes in the stratum var like missing values or a third
category).

So the question is if there are more convenient ways to calculate aggregate
constants and store them in locals.

Best wishes

Stefan

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