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









New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you found my answer helpful, please also consider up-voting it using the upper arrow.
    – Pearly Spencer
    yesterday















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









New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you found my answer helpful, please also consider up-voting it using the upper arrow.
    – Pearly Spencer
    yesterday













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









New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 15:39









Pearly Spencer

8,504163250




8,504163250






New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 15:26









Steve

162




162




New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • If you found my answer helpful, please also consider up-voting it using the upper arrow.
    – Pearly Spencer
    yesterday


















  • If you found my answer helpful, please also consider up-voting it using the upper arrow.
    – Pearly Spencer
    yesterday
















If you found my answer helpful, please also consider up-voting it using the upper arrow.
– Pearly Spencer
yesterday




If you found my answer helpful, please also consider up-voting it using the upper arrow.
– Pearly Spencer
yesterday












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





















    • I had forgotten about tabstat!
      – Pearly Spencer
      Nov 12 at 15:21










    • Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
      – Nick Cox
      Nov 12 at 16:34










    • There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
      – Pearly Spencer
      Nov 12 at 16:56










    • What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
      – Pearly Spencer
      Nov 12 at 16:57










    • if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
      – Nick Cox
      Nov 12 at 17:04











    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
    });


    }
    });






    Steve is a new contributor. Be nice, and check out our Code of Conduct.










     

    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

        8,504163250




        8,504163250
























            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





















            • I had forgotten about tabstat!
              – Pearly Spencer
              Nov 12 at 15:21










            • Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
              – Nick Cox
              Nov 12 at 16:34










            • There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
              – Pearly Spencer
              Nov 12 at 16:56










            • What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
              – Pearly Spencer
              Nov 12 at 16:57










            • if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
              – Nick Cox
              Nov 12 at 17:04















            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





















            • I had forgotten about tabstat!
              – Pearly Spencer
              Nov 12 at 15:21










            • Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
              – Nick Cox
              Nov 12 at 16:34










            • There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
              – Pearly Spencer
              Nov 12 at 16:56










            • What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
              – Pearly Spencer
              Nov 12 at 16:57










            • if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
              – Nick Cox
              Nov 12 at 17:04













            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.4k42036




            24.4k42036












            • I had forgotten about tabstat!
              – Pearly Spencer
              Nov 12 at 15:21










            • Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
              – Nick Cox
              Nov 12 at 16:34










            • There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
              – Pearly Spencer
              Nov 12 at 16:56










            • What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
              – Pearly Spencer
              Nov 12 at 16:57










            • if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
              – Nick Cox
              Nov 12 at 17:04


















            • I had forgotten about tabstat!
              – Pearly Spencer
              Nov 12 at 15:21










            • Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
              – Nick Cox
              Nov 12 at 16:34










            • There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
              – Pearly Spencer
              Nov 12 at 16:56










            • What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
              – Pearly Spencer
              Nov 12 at 16:57










            • if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
              – Nick Cox
              Nov 12 at 17:04
















            I had forgotten about tabstat!
            – Pearly Spencer
            Nov 12 at 15:21




            I had forgotten about tabstat!
            – Pearly Spencer
            Nov 12 at 15:21












            Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
            – Nick Cox
            Nov 12 at 16:34




            Stata's tabulation commands are a bit of a mess. But producing a table isn't exactly a small or well-defined task. What of? is only the start.
            – Nick Cox
            Nov 12 at 16:34












            There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
            – Pearly Spencer
            Nov 12 at 16:56




            There's considerable overlap between commands but that's not necessarily a bad thing. With tables it is hard to create a command to cater for all use cases. If you want to deviate even slightly from what is offered you need to program everything yourself.
            – Pearly Spencer
            Nov 12 at 16:56












            What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
            – Pearly Spencer
            Nov 12 at 16:57




            What I find really annoying is the fact that by does not return results from all groups but only the most recent one. How hard could had been for StataCorp to program it to return everything?
            – Pearly Spencer
            Nov 12 at 16:57












            if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
            – Nick Cox
            Nov 12 at 17:04




            if that's really annoying then I don't where your scale extends to! But say you run 100000 regressions using by:. What do you expect to be available after them all? Stata provides lots of ways to save the results of 100000 regressions; just don't expect 100000 sets of results, including 100000 copies of e(sample), all to remain accessible. Conversely, letting r- and e-class estimates pile up isn't really in anyone's best interests. You still would have to write code to process them all.
            – Nick Cox
            Nov 12 at 17:04










            Steve is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Steve is a new contributor. Be nice, and check out our Code of Conduct.













            Steve is a new contributor. Be nice, and check out our Code of Conduct.












            Steve is a new contributor. Be nice, and check out our Code of Conduct.















             


            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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues