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?
stata
add a comment |
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?
stata
add a comment |
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?
stata
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
stata
edited Nov 10 at 15:39
Pearly Spencer
9,528173353
9,528173353
asked Nov 10 at 15:26
Steve
163
163
add a comment |
add a comment |
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.
add a comment |
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
-------------------------
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 10 at 15:47
answered Nov 10 at 15:35
Pearly Spencer
9,528173353
9,528173353
add a comment |
add a comment |
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
-------------------------
add a comment |
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
-------------------------
add a comment |
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
-------------------------
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
-------------------------
answered Nov 11 at 1:52
Nick Cox
24.8k42038
24.8k42038
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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