Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating mean for a same value with two different variables

    Complete undergraduate newbie with Stata so apologies in advance

    I have the following dataset
    Country Region Age
    United Kingdom 1 10
    United Kingdom 2 12
    United States 1 10
    United States 2 12
    What command should I put if I want to ignore the region and calculate the mean age for each country?

    Thank you in adanvce

  • #2
    Code:
    by country, sort: egen mean_age = mean(age)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you for the advice, I will definitely keep that in mind when asking another question.


      I understand that I now have a new variable called mean_age. However, how would I extract the mean age for all the regions in the United States?

      Comment


      • #4
        Are you contradicting your question in #1, in which you specifically say you want to ignore region, or is this a new question? Assuming the latter:

        Code:
        by region, sort: egen regional_mean_age = mean(age) if country == "United States"
        Note: this assumes that country is a string variable. If it is actually a value-labeled numeric variable, you will get a type mismatch error message from this code, and no results. I cannot tell from your tableau what kind of variable country is. This is the reason for using -dataex-. So if you are going to continue with more questions in this thread, you need to post example data with -dataex-.

        Since you are a self-described newbie, I think it is time for you to step back from trying to produce results and acquaint yourself with the basics of Stata. Your Stata installation includes PDF manuals, which you can access from the Help menu in Stata. Invest some time and effort in reading the Getting Started [GS] and User's Guide [U] volumes. It's a fairly long read, but it will introduce you to the fundamental aspects of Stata that every user needs to know in order to work effectively. You won't remember every detail--nobody does, even the experts have to refer back to the documentation or help files to fill in details from time to time. But with the knowledge you gain there under your belt, you will be able to make steady progress in using Stata, without having to seek outside assistance every step of the way. The time you invest in this reading will be amply repaid rapidly.

        Comment

        Working...
        X