ThesisStudent
ThesisStudent

Reputation: 39

calculate mean for groups

I'm doing a research about SATs scores by cities. I have every city coded. Is there any way to ask Stata to calculate the mean for each city without having to type the city-code? Not having to write: sum if city==1 and then sum if city==2, but instead that Stata takes all the people with the same city code and calculates the mean?

Upvotes: 0

Views: 910

Answers (2)

dimitriy
dimitriy

Reputation: 9460

There are several ways to skin a cat, but something like this would do it:

table city_name, contents(mean sat_score count sat_score) row

This also gives you the number of individuals with non-missing scores in each city as well as an overall average. Type help table for other options.

sum also takes a bysort prefix, so bysort city_name: sum sat_score also works, but the output is less compact than with table.

Unlike the egen approach, neither of these will create a variable that stores the city-specific means. It will only show you the output on the screen.

Upvotes: 1

Duncan
Duncan

Reputation: 151

It looks like this post on stack overflow has some code it in you could use:

Using if qualifier with egen in Stata

Upvotes: 1

Related Questions