Announcement

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

  • Create a loop

    Dear Stata Users,

    I would like to create a loop but i have some difficulty to deal with it.

    Here is an overview of my data. I have an id for every firms, a variable for 'va' and for the 'sector' to which the firm belong.

    I would like to generate the variable demeaning_va_sector. This variable is the difference between the variable 'va' and the average of the sector for the 'va' to which the company belongs.

    For example, for the first sector, the mean is (600+400+500)/3 = 500. So the demeaning_va is (600-500) =100
    id va sector demeaning_va_sector
    1 600 1 100 ==>( 600-500)
    2 300 2 -150 ==>(300 - 450)
    3 400 1 -100
    4 500 1 0
    5 600 2 150
    6 700 3 300
    7 100 3 -300
    I have more than 20000 firms and i have 78 sectors.

    Thanks for your help,

    Pierre

  • #2
    Pierre,
    I do not think you need a loop. Here is one way

    Code:
    clear
    input  id    va   sector  
         1   600        1  
         2   300        2  
         3   400        1  
         4   500        1  
         5   600        2  
         6   700        3  
         7   100        3  
    end
    
    bys sector : egen sector_avg = mean(va)
    gen demeaning_va_sector = va-sector_avg
    list
    010100100110111101101110011011100110100101100101

    Comment


    • #3
      Thank you Ronnie

      Comment

      Working...
      X