Announcement

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

  • Rank countries in order of average GDP

    Hi, I want to do something very very simple and have tried searching statalist for the command that will produce it. I have generated the average GDP for each country and would now like to list the countries in order of highest GDP to the least. I think all I need is one command after this:

    egen meanGDP = mean(GDP), by (country)

    Any advice would be appreciated. Thanks in advance.

  • #2
    You say "average GDP", does this mean you have several different values (eg, years) for each country? Please describe your data.

    Comment


    • #3
      I think this should work. Assuming you have the mean for countries (meanGDP).

      Code:
      gsort -meanGDP /*the minus sign (-) before meanGDP sorts the variable in decending order*/
      list country
      Roman

      Comment


      • #4
        Roman, thank you!
        Jeph, I have several years of data, and have used the egen command to calculate one average of those years. However I've just discovered that it then applies to the country over and over, so when I list, I get

        1. Luxembourg
        2. Luxembourg
        3. Luxembourg

        I would like each country to just appear once. Thank you both for your replies.

        Comment


        • #5
          Please note Jeph's suggestion. If you have countries repeated in the dataset, the list command will produce repeated data for the countries. Best for you is to provide a sub sample of your data.
          Roman

          Comment


          • #6
            This is a small sub-subsample of the layout of the data. I would like to just make a simple table with one value for each country, ranked in descending order. I could do it in excel but don't know how to do it in STATA
            country s006 continent_n meanGDP
            Albania 200656 europe 3.437574
            Albania 152 europe 3.437574
            Albania 1190 europe 3.437574
            Albania 534 europe 3.437574
            Albania 553 europe 3.437574
            Albania 524 europe 3.437574
            Albania 1041 europe 3.437574
            Albania 1091 europe 3.437574

            Comment


            • #7
              You need to creat a variable that identifies country just once and individually.

              Code:
              gsort -meanGDP
              egen tag_country=tag(country)
              list country if tag_country==1
              Roman

              Comment


              • #8
                or, since meanGDP is the same for each observation within country:

                Code:
                bysort country: keep if _n==1
                drops potentially useful cases, but can either preserve/restore, or just reload original data.

                Comment


                • #9
                  better yet, no cases lost:
                  Code:
                  tabulate country, summarize(GDP)
                  Last edited by ben earnhart; 15 Nov 2014, 16:42. Reason: coma not colon

                  Comment


                  • #10
                    Thanks so much for your help! That's the first step at least ... I know there are plenty more things to confuse me yet

                    Comment

                    Working...
                    X