Announcement

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

  • trying to sum with different indeces

    I have a data similar to one below:
    Country code-Value
    1 -10
    1-12
    1-15
    2-16
    2-19
    3-35
    3-23
    3-56
    3-12
    4-34
    4-56
    What I need to do is to sum the values with the same country codes. How can I do it?

  • #2
    Assuming your actual data set looks something like the one generated in the code below, it is straight forward.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(countrycode value)
    1 10
    1 12
    1 15
    2 16
    2 19
    3 35
    3 23
    3 56
    3 12
    4 34
    4 56
    end
    
    by countrycode, sort: egen value_total = total(value)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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
      Cross-posted at https://www.reddit.com/r/stata/comme..._same_indexes/

      Please note our policy on cross-posting, which is that you are asked to tell us about it. This is explained at https://www.statalist.org/forums/help#crossposting

      Comment

      Working...
      X