Announcement

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

  • Aggregate data by TWO groups

    Hey everyone, I'm new to Stata and have a question.

    I need to aggregate the data by year and company. With the collapse function, I can only sum up the values for the companies for all years.




    The raw data look like this:

    id year companyid sales
    1 2010 555 50,000
    2 2010 555 46,000
    3 2010 555 40,000
    4 2011 555 30,000
    5 2011 555 51,000
    6 2010 700 2,000
    7 2010 700 4,000
    8 2011 700 7,000
    9 2011 700 11,000
    10 2011 700 5,000
    .....


    The result I need, should look like this:

    year companyid Sum
    2010 555 136,000
    2010 700 6,000
    2011 555 81,000
    2011 700 23,000
    .....


    I appreciate any thoughts, many thanks!
    Last edited by Tobias Winkelmann; 07 May 2023, 09:42.

  • #2
    I can follow what is wanted, but not why you can't get it. Strictly, collapse is a command, and not a function. In Stata function is not another word for command.

    Code:
    clear 
    input id year companyid sales
    1 2010 555 50.000
    2 2010 555 46.000
    3 2010 555 40.000
    4 2011 555 30.000
    5 2011 555 51.000
    6 2010 700 2.000
    7 2010 700 4.000
    8 2011 700 7.000
    9 2011 700 11.000
    10 2011 700 5.000
    end 
    
    collapse (sum) sales, by(companyid year)
    
    list 
    
         +-------------------------+
         | year   compan~d   sales |
         |-------------------------|
      1. | 2010        555     136 |
      2. | 2011        555      81 |
      3. | 2010        700       6 |
      4. | 2011        700      23 |
         +-------------------------+

    Comment


    • #3
      thank you very much! I did not know that I can use collapse with two variables at the same time...

      Comment


      • #4
        Take a look at

        Code:
        help collapse
        which includes examples to that effect.

        Comment

        Working...
        X