Calculate the sum of a variable









up vote
2
down vote

favorite












I would like to calculate the sum of variable boasav:



clear

input id boasav
1 2500
1 2900
1 4200
2 5700
2 6100
3 7400
3 7600
3 8300
end


I know that the tabulate command can be used to summarize data but it only counts:



bys id: tab boasav 

-> id = 1

boasav | Freq. Percent Cum.
------------+-----------------------------------
2500 | 1 33.33 33.33
2900 | 1 33.33 66.67
4200 | 1 33.33 100.00
------------+-----------------------------------
Total | 3 100.00

-> id = 2

boasav | Freq. Percent Cum.
------------+-----------------------------------
5700 | 1 50.00 50.00
6100 | 1 50.00 100.00
------------+-----------------------------------
Total | 2 100.00

-> id = 3

boasav | Freq. Percent Cum.
------------+-----------------------------------
7400 | 1 33.33 33.33
7600 | 1 33.33 66.67
8300 | 1 33.33 100.00
------------+-----------------------------------
Total | 3 100.00


However, what I want is the following:



1 9600 
2 11800
3 23300


Is there a function that can do this in Stata?










share|improve this question



























    up vote
    2
    down vote

    favorite












    I would like to calculate the sum of variable boasav:



    clear

    input id boasav
    1 2500
    1 2900
    1 4200
    2 5700
    2 6100
    3 7400
    3 7600
    3 8300
    end


    I know that the tabulate command can be used to summarize data but it only counts:



    bys id: tab boasav 

    -> id = 1

    boasav | Freq. Percent Cum.
    ------------+-----------------------------------
    2500 | 1 33.33 33.33
    2900 | 1 33.33 66.67
    4200 | 1 33.33 100.00
    ------------+-----------------------------------
    Total | 3 100.00

    -> id = 2

    boasav | Freq. Percent Cum.
    ------------+-----------------------------------
    5700 | 1 50.00 50.00
    6100 | 1 50.00 100.00
    ------------+-----------------------------------
    Total | 2 100.00

    -> id = 3

    boasav | Freq. Percent Cum.
    ------------+-----------------------------------
    7400 | 1 33.33 33.33
    7600 | 1 33.33 66.67
    8300 | 1 33.33 100.00
    ------------+-----------------------------------
    Total | 3 100.00


    However, what I want is the following:



    1 9600 
    2 11800
    3 23300


    Is there a function that can do this in Stata?










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I would like to calculate the sum of variable boasav:



      clear

      input id boasav
      1 2500
      1 2900
      1 4200
      2 5700
      2 6100
      3 7400
      3 7600
      3 8300
      end


      I know that the tabulate command can be used to summarize data but it only counts:



      bys id: tab boasav 

      -> id = 1

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      2500 | 1 33.33 33.33
      2900 | 1 33.33 66.67
      4200 | 1 33.33 100.00
      ------------+-----------------------------------
      Total | 3 100.00

      -> id = 2

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      5700 | 1 50.00 50.00
      6100 | 1 50.00 100.00
      ------------+-----------------------------------
      Total | 2 100.00

      -> id = 3

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      7400 | 1 33.33 33.33
      7600 | 1 33.33 66.67
      8300 | 1 33.33 100.00
      ------------+-----------------------------------
      Total | 3 100.00


      However, what I want is the following:



      1 9600 
      2 11800
      3 23300


      Is there a function that can do this in Stata?










      share|improve this question















      I would like to calculate the sum of variable boasav:



      clear

      input id boasav
      1 2500
      1 2900
      1 4200
      2 5700
      2 6100
      3 7400
      3 7600
      3 8300
      end


      I know that the tabulate command can be used to summarize data but it only counts:



      bys id: tab boasav 

      -> id = 1

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      2500 | 1 33.33 33.33
      2900 | 1 33.33 66.67
      4200 | 1 33.33 100.00
      ------------+-----------------------------------
      Total | 3 100.00

      -> id = 2

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      5700 | 1 50.00 50.00
      6100 | 1 50.00 100.00
      ------------+-----------------------------------
      Total | 2 100.00

      -> id = 3

      boasav | Freq. Percent Cum.
      ------------+-----------------------------------
      7400 | 1 33.33 33.33
      7600 | 1 33.33 66.67
      8300 | 1 33.33 100.00
      ------------+-----------------------------------
      Total | 3 100.00


      However, what I want is the following:



      1 9600 
      2 11800
      3 23300


      Is there a function that can do this in Stata?







      stata






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 15:39









      Pearly Spencer

      9,528173353




      9,528173353










      asked Nov 10 at 15:26









      Steve

      163




      163






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Solution 1: calculate and present using the list or table commands



          bysort id: list, sum(boasav)

          -> id = 1
          +-------------+
          | id boasav |
          |-------------|
          1. | 1 2500 |
          2. | 1 2900 |
          3. | 1 4200 |
          |-------------|
          Sum | 9600 |
          +-------------+

          -> id = 2
          +-------------+
          | id boasav |
          |-------------|
          1. | 2 5700 |
          2. | 2 6100 |
          |-------------|
          Sum | 11800 |
          +-------------+

          -> id = 3
          +-------------+
          | id boasav |
          |-------------|
          1. | 3 7400 |
          2. | 3 7600 |
          3. | 3 8300 |
          |-------------|
          Sum | 23300 |
          +-------------+




          table id, contents(sum boasav)

          -----------------------
          id | sum(boasav)
          ----------+------------
          1 | 9600
          2 | 11800
          3 | 23300
          -----------------------


          Solution 2: generate extra variables with the results and then list



          bysort id (boasav): generate sum1 = sum(boasav)


          or



          by id: egen sum2 = total(boasav)


          Both of these approaches will produce the same results:



          by id: list sum* if _n == _N

          -> id = 1
          +-------------+
          | sum1 sum2 |
          |-------------|
          3. | 9600 9600 |
          +-------------+

          -> id = 2
          +---------------+
          | sum1 sum2 |
          |---------------|
          2. | 11800 11800 |
          +---------------+

          -> id = 3
          +---------------+
          | sum1 sum2 |
          |---------------|
          3. | 23300 23300 |
          +---------------+


          Solution 3: create a new dataset with the results and list



          collapse (sum) boasav, by(id)
          list

          +-------------+
          | id boasav |
          |-------------|
          1. | 1 9600 |
          2. | 2 11800 |
          3. | 3 23300 |
          +-------------+


          Note that this last solution will destroy your current dataset.






          share|improve this answer





























            up vote
            1
            down vote













            Here are three more.



            clear

            input id boasav
            1 2500
            1 2900
            1 4200
            2 5700
            2 6100
            3 7400
            3 7600
            3 8300
            end

            * Method 4: use summarize

            forval g = 1/3
            su boasav if id == `g', meanonly
            di "`g' " %5.0f r(sum)


            1 9600
            2 11800
            3 23300


            * Method 5: tabstat

            tabstat boasav, by(id) stat(sum)

            Summary for variables: boasav
            by categories of: id

            id | sum
            ---------+----------
            1 | 9600
            2 | 11800
            3 | 23300
            ---------+----------
            Total | 44700
            --------------------


            * Method 6: use rangestat (SSC)

            rangestat (sum) boasav, int(id 0 0)

            tabdisp id, c(boasav_sum)

            -------------------------
            id | sum of boasav
            ----------+--------------
            1 | 9600
            2 | 11800
            3 | 23300
            -------------------------





            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240407%2fcalculate-the-sum-of-a-variable%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Solution 1: calculate and present using the list or table commands



              bysort id: list, sum(boasav)

              -> id = 1
              +-------------+
              | id boasav |
              |-------------|
              1. | 1 2500 |
              2. | 1 2900 |
              3. | 1 4200 |
              |-------------|
              Sum | 9600 |
              +-------------+

              -> id = 2
              +-------------+
              | id boasav |
              |-------------|
              1. | 2 5700 |
              2. | 2 6100 |
              |-------------|
              Sum | 11800 |
              +-------------+

              -> id = 3
              +-------------+
              | id boasav |
              |-------------|
              1. | 3 7400 |
              2. | 3 7600 |
              3. | 3 8300 |
              |-------------|
              Sum | 23300 |
              +-------------+




              table id, contents(sum boasav)

              -----------------------
              id | sum(boasav)
              ----------+------------
              1 | 9600
              2 | 11800
              3 | 23300
              -----------------------


              Solution 2: generate extra variables with the results and then list



              bysort id (boasav): generate sum1 = sum(boasav)


              or



              by id: egen sum2 = total(boasav)


              Both of these approaches will produce the same results:



              by id: list sum* if _n == _N

              -> id = 1
              +-------------+
              | sum1 sum2 |
              |-------------|
              3. | 9600 9600 |
              +-------------+

              -> id = 2
              +---------------+
              | sum1 sum2 |
              |---------------|
              2. | 11800 11800 |
              +---------------+

              -> id = 3
              +---------------+
              | sum1 sum2 |
              |---------------|
              3. | 23300 23300 |
              +---------------+


              Solution 3: create a new dataset with the results and list



              collapse (sum) boasav, by(id)
              list

              +-------------+
              | id boasav |
              |-------------|
              1. | 1 9600 |
              2. | 2 11800 |
              3. | 3 23300 |
              +-------------+


              Note that this last solution will destroy your current dataset.






              share|improve this answer


























                up vote
                1
                down vote



                accepted










                Solution 1: calculate and present using the list or table commands



                bysort id: list, sum(boasav)

                -> id = 1
                +-------------+
                | id boasav |
                |-------------|
                1. | 1 2500 |
                2. | 1 2900 |
                3. | 1 4200 |
                |-------------|
                Sum | 9600 |
                +-------------+

                -> id = 2
                +-------------+
                | id boasav |
                |-------------|
                1. | 2 5700 |
                2. | 2 6100 |
                |-------------|
                Sum | 11800 |
                +-------------+

                -> id = 3
                +-------------+
                | id boasav |
                |-------------|
                1. | 3 7400 |
                2. | 3 7600 |
                3. | 3 8300 |
                |-------------|
                Sum | 23300 |
                +-------------+




                table id, contents(sum boasav)

                -----------------------
                id | sum(boasav)
                ----------+------------
                1 | 9600
                2 | 11800
                3 | 23300
                -----------------------


                Solution 2: generate extra variables with the results and then list



                bysort id (boasav): generate sum1 = sum(boasav)


                or



                by id: egen sum2 = total(boasav)


                Both of these approaches will produce the same results:



                by id: list sum* if _n == _N

                -> id = 1
                +-------------+
                | sum1 sum2 |
                |-------------|
                3. | 9600 9600 |
                +-------------+

                -> id = 2
                +---------------+
                | sum1 sum2 |
                |---------------|
                2. | 11800 11800 |
                +---------------+

                -> id = 3
                +---------------+
                | sum1 sum2 |
                |---------------|
                3. | 23300 23300 |
                +---------------+


                Solution 3: create a new dataset with the results and list



                collapse (sum) boasav, by(id)
                list

                +-------------+
                | id boasav |
                |-------------|
                1. | 1 9600 |
                2. | 2 11800 |
                3. | 3 23300 |
                +-------------+


                Note that this last solution will destroy your current dataset.






                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Solution 1: calculate and present using the list or table commands



                  bysort id: list, sum(boasav)

                  -> id = 1
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 1 2500 |
                  2. | 1 2900 |
                  3. | 1 4200 |
                  |-------------|
                  Sum | 9600 |
                  +-------------+

                  -> id = 2
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 2 5700 |
                  2. | 2 6100 |
                  |-------------|
                  Sum | 11800 |
                  +-------------+

                  -> id = 3
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 3 7400 |
                  2. | 3 7600 |
                  3. | 3 8300 |
                  |-------------|
                  Sum | 23300 |
                  +-------------+




                  table id, contents(sum boasav)

                  -----------------------
                  id | sum(boasav)
                  ----------+------------
                  1 | 9600
                  2 | 11800
                  3 | 23300
                  -----------------------


                  Solution 2: generate extra variables with the results and then list



                  bysort id (boasav): generate sum1 = sum(boasav)


                  or



                  by id: egen sum2 = total(boasav)


                  Both of these approaches will produce the same results:



                  by id: list sum* if _n == _N

                  -> id = 1
                  +-------------+
                  | sum1 sum2 |
                  |-------------|
                  3. | 9600 9600 |
                  +-------------+

                  -> id = 2
                  +---------------+
                  | sum1 sum2 |
                  |---------------|
                  2. | 11800 11800 |
                  +---------------+

                  -> id = 3
                  +---------------+
                  | sum1 sum2 |
                  |---------------|
                  3. | 23300 23300 |
                  +---------------+


                  Solution 3: create a new dataset with the results and list



                  collapse (sum) boasav, by(id)
                  list

                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 1 9600 |
                  2. | 2 11800 |
                  3. | 3 23300 |
                  +-------------+


                  Note that this last solution will destroy your current dataset.






                  share|improve this answer














                  Solution 1: calculate and present using the list or table commands



                  bysort id: list, sum(boasav)

                  -> id = 1
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 1 2500 |
                  2. | 1 2900 |
                  3. | 1 4200 |
                  |-------------|
                  Sum | 9600 |
                  +-------------+

                  -> id = 2
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 2 5700 |
                  2. | 2 6100 |
                  |-------------|
                  Sum | 11800 |
                  +-------------+

                  -> id = 3
                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 3 7400 |
                  2. | 3 7600 |
                  3. | 3 8300 |
                  |-------------|
                  Sum | 23300 |
                  +-------------+




                  table id, contents(sum boasav)

                  -----------------------
                  id | sum(boasav)
                  ----------+------------
                  1 | 9600
                  2 | 11800
                  3 | 23300
                  -----------------------


                  Solution 2: generate extra variables with the results and then list



                  bysort id (boasav): generate sum1 = sum(boasav)


                  or



                  by id: egen sum2 = total(boasav)


                  Both of these approaches will produce the same results:



                  by id: list sum* if _n == _N

                  -> id = 1
                  +-------------+
                  | sum1 sum2 |
                  |-------------|
                  3. | 9600 9600 |
                  +-------------+

                  -> id = 2
                  +---------------+
                  | sum1 sum2 |
                  |---------------|
                  2. | 11800 11800 |
                  +---------------+

                  -> id = 3
                  +---------------+
                  | sum1 sum2 |
                  |---------------|
                  3. | 23300 23300 |
                  +---------------+


                  Solution 3: create a new dataset with the results and list



                  collapse (sum) boasav, by(id)
                  list

                  +-------------+
                  | id boasav |
                  |-------------|
                  1. | 1 9600 |
                  2. | 2 11800 |
                  3. | 3 23300 |
                  +-------------+


                  Note that this last solution will destroy your current dataset.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 15:47

























                  answered Nov 10 at 15:35









                  Pearly Spencer

                  9,528173353




                  9,528173353






















                      up vote
                      1
                      down vote













                      Here are three more.



                      clear

                      input id boasav
                      1 2500
                      1 2900
                      1 4200
                      2 5700
                      2 6100
                      3 7400
                      3 7600
                      3 8300
                      end

                      * Method 4: use summarize

                      forval g = 1/3
                      su boasav if id == `g', meanonly
                      di "`g' " %5.0f r(sum)


                      1 9600
                      2 11800
                      3 23300


                      * Method 5: tabstat

                      tabstat boasav, by(id) stat(sum)

                      Summary for variables: boasav
                      by categories of: id

                      id | sum
                      ---------+----------
                      1 | 9600
                      2 | 11800
                      3 | 23300
                      ---------+----------
                      Total | 44700
                      --------------------


                      * Method 6: use rangestat (SSC)

                      rangestat (sum) boasav, int(id 0 0)

                      tabdisp id, c(boasav_sum)

                      -------------------------
                      id | sum of boasav
                      ----------+--------------
                      1 | 9600
                      2 | 11800
                      3 | 23300
                      -------------------------





                      share|improve this answer
























                        up vote
                        1
                        down vote













                        Here are three more.



                        clear

                        input id boasav
                        1 2500
                        1 2900
                        1 4200
                        2 5700
                        2 6100
                        3 7400
                        3 7600
                        3 8300
                        end

                        * Method 4: use summarize

                        forval g = 1/3
                        su boasav if id == `g', meanonly
                        di "`g' " %5.0f r(sum)


                        1 9600
                        2 11800
                        3 23300


                        * Method 5: tabstat

                        tabstat boasav, by(id) stat(sum)

                        Summary for variables: boasav
                        by categories of: id

                        id | sum
                        ---------+----------
                        1 | 9600
                        2 | 11800
                        3 | 23300
                        ---------+----------
                        Total | 44700
                        --------------------


                        * Method 6: use rangestat (SSC)

                        rangestat (sum) boasav, int(id 0 0)

                        tabdisp id, c(boasav_sum)

                        -------------------------
                        id | sum of boasav
                        ----------+--------------
                        1 | 9600
                        2 | 11800
                        3 | 23300
                        -------------------------





                        share|improve this answer






















                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Here are three more.



                          clear

                          input id boasav
                          1 2500
                          1 2900
                          1 4200
                          2 5700
                          2 6100
                          3 7400
                          3 7600
                          3 8300
                          end

                          * Method 4: use summarize

                          forval g = 1/3
                          su boasav if id == `g', meanonly
                          di "`g' " %5.0f r(sum)


                          1 9600
                          2 11800
                          3 23300


                          * Method 5: tabstat

                          tabstat boasav, by(id) stat(sum)

                          Summary for variables: boasav
                          by categories of: id

                          id | sum
                          ---------+----------
                          1 | 9600
                          2 | 11800
                          3 | 23300
                          ---------+----------
                          Total | 44700
                          --------------------


                          * Method 6: use rangestat (SSC)

                          rangestat (sum) boasav, int(id 0 0)

                          tabdisp id, c(boasav_sum)

                          -------------------------
                          id | sum of boasav
                          ----------+--------------
                          1 | 9600
                          2 | 11800
                          3 | 23300
                          -------------------------





                          share|improve this answer












                          Here are three more.



                          clear

                          input id boasav
                          1 2500
                          1 2900
                          1 4200
                          2 5700
                          2 6100
                          3 7400
                          3 7600
                          3 8300
                          end

                          * Method 4: use summarize

                          forval g = 1/3
                          su boasav if id == `g', meanonly
                          di "`g' " %5.0f r(sum)


                          1 9600
                          2 11800
                          3 23300


                          * Method 5: tabstat

                          tabstat boasav, by(id) stat(sum)

                          Summary for variables: boasav
                          by categories of: id

                          id | sum
                          ---------+----------
                          1 | 9600
                          2 | 11800
                          3 | 23300
                          ---------+----------
                          Total | 44700
                          --------------------


                          * Method 6: use rangestat (SSC)

                          rangestat (sum) boasav, int(id 0 0)

                          tabdisp id, c(boasav_sum)

                          -------------------------
                          id | sum of boasav
                          ----------+--------------
                          1 | 9600
                          2 | 11800
                          3 | 23300
                          -------------------------






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 1:52









                          Nick Cox

                          24.8k42038




                          24.8k42038



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240407%2fcalculate-the-sum-of-a-variable%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20