Search code examples
groupingstatamedian

Display median / mean


I'm new to Stata and have been through the help files and did some searches but I can't find what I'm looking for. I want to see the median of a variable I created by each group without having to list the detail.

I have a binary variable with values 1 or 0 called groupbyvar and the variable I want the median for var1

my code:

bysort groupbyvar: egen median_var1 = median(var1)

This works but now I want to see two values one for groupbyvar = 0 and one for groupbyvar = 1 respectively. How do I achieve this?

If I use list groupbyvar median_var1 I get what I want but I have to go through all the data and if I use display median_var1 I only see the value for one of the groupbyvar's values


Solution

  • Given what you have done, this would give a minimal display

    tabdisp bygroupvar, c(median_var1) 
    

    and indeed if all you want is a display, there is no need to create a variable first.

    Here is a reproducible example

    . sysuse auto, clear
    
    . tabstat mpg, s(median) by(foreign)
    
    Summary for variables: mpg
         by categories of: foreign (Car type)
    
     foreign |       p50
    ---------+----------
    Domestic |        19
     Foreign |      24.5
    ---------+----------
       Total |        20
    --------------------
    

    and study of the help for tabstat would show the scope for looking at single summary statistics, as you request, and at several.