Announcement

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

  • Calculating weighted totals (sums) of a variable by groups (levels) of another variable

    Hello everybody;
    I'am new in this forum. I've studied Stata at my university for about 2 years, and now i'am preparing a paper for my graduation. And I needed Stata to deal with a 25000 observation dataset
    Here's a little of it:

    id region salaries weight
    1 1 1200 23.15
    2 1 500 7.65
    3 2 100 10.12
    4 2 1700 2.95
    5 3 2050 14.00
    6 3 1435 3.60

    My goal is to calculate weighted totals (sums) of salaries by region
    I've tried most of the commun commands: bysort egen, bysort asgen (bysort doesn't allow weights), i've tried collapse but i didn't understand how to use it with all it's options for my kinda of data and goal

    I would be so happy to hear your suggestions about a Stata comman or algorithm that could help me get the total of salaries, by region, considering the weights

    THANKS VERY MUCH !

  • #2
    Code:
     
     bys region: egen wanted = total(weight * salaries)

    Comment


    • #3
      Thanks for your suggestion M. Cox. It did work for me, i just don't have that much confidence in my results since i'am working with a 25000 observation dataset. Yet, I thought, as it's a classic rule with Stata, I thought the weight variable should be put between brackets, like [aw=weight], so why incorporate it in the total formula ? Or is the the same thing as putting it beween brackets ?

      Comment


      • #4
        egen does not support weights.

        Comment


        • #5
          So, is there any other method or command to generate weighted totals of a variable by levels of another variable then ? I've tried asgen, i didn't work too

          Comment


          • #6
            #2 is a solution. You can do it in a more long-winded way if you want. Here is one other way.


            Code:
            bys region: gen double wanted = sum(weight * salaries)
            by region: replace wanted = wanted[_N]
            double is also a good idea in #2,
            Last edited by Nick Cox; 05 Apr 2020, 01:58.

            Comment


            • #7
              Ok. Thanks very much for your help

              Comment


              • #8
                Hello everybody ;

                I'm working with some data on Excel, the Excel fille has like 5 papers on it, the observations on all those papers are defined by the same ID number (the unique key)
                I wanna merge those 5 Excel papers on Stata using the command merge, I just don't know the option on the command that will tell Stata those 5 papers are in the same Excel file.

                Can anyone help please ?

                Thanks very much

                Comment

                Working...
                X