Announcement

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

  • combine Several observations to one observation in a given year

    Dear all,

    I need to get the SUM of Var 1. for each ID observation in 2016,

    for example, A person who has ID 1 has a total of $100 for Var.1 and $500 for Var. 2 in 2016, and so on

    I need to have one observation for each ID , by summing up the observations for this ID in this year

    What is the command should I use ?
    ID Year Var. 1 Var. 2
    1 2016 50 100
    1 2016 30 200
    1 2016 20 300
    2 2016 60
    2 2016
    2 2016
    1 2017
    1 2017
    1 2017
    Last edited by Mohamed Mahmoud; 18 May 2022, 16:22.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year byte var1 int var2
    1 2016 50 100
    1 2016 30 200
    1 2016 20 300
    2 2016 60  42
    2 2016 40  58
    end
    
    collapse (sum) var1 var2, by(id year)
    
    list
    
         +-------------------------+
         | id   year   var1   var2 |
         |-------------------------|
      1. |  1   2016    100    600 |
      2. |  2   2016    100    100 |
         +-------------------------+

    Comment


    • #3
      Thanks Nick Cox,

      one more question please

      What is the code that used to generate variable called " average assets" ffirm-yearirm year observation

      Note: Average assets = (assets current year + assets previous year) / 2


      the data format is
      firm ID year total assets average assets
      1 2010 10 .
      1 2011 20 = 15
      1 2012 25 =22.5
      2 2010 30 =.
      2 2011 50 =40
      2 2012 80 = 65

      Many thanks

      Comment

      Working...
      X