Home > OS >  How to compute the sum of some variables in Stata?
How to compute the sum of some variables in Stata?

Time:09-12

I have 48 variables in my dataset: first 12 concern year 2000, second 12 year 2001, third 12 year 2002 and fourth 12 year 2003. Each single variable contains the values in such a way:

ID var1 var2 var3 ... var12 ... var48
xx 0 0 1 ... 1 ... 0
yy 1 0 0 ... 9 ... 0
zz 3 2 1 ... 0 ... 0

Now, I want to collect the sum of the values of the first 12 variables in another one called, say, "tot_2000" which should contain just one number (in this example it is 18). Then, I must repeat this passage for the 3 remaining years, thus having 4 variables ("tot_2000", "tot_2001", "tot2002", "tot2003") to be plotted in an histogram.

What I'm looking for is such a variable:

tot_2000
18

CodePudding user response:

Here is a solution for how to do it in two steps:

* Example generated by -dataex-. For more info, type help dataex
clear
input str2 ID byte(var1 var2 var3 var4)
"xx" 0 0 1 1
"yy" 1 0 0 9
"zz" 3 2 1 0
end

egen row_sum = rowtotal(var*) //Sum each row into a var
egen tot_var = sum(row_sum ) //Sum the row_sum var

* Get the value of the first observation and store in a local macro
local total = tot_var[1]
display `total'

CodePudding user response:

Here is another way to do it, as indicated in a comment on the first answer.

* Example generated by -dataex-. For more info, type help dataex
clear
input str2 ID byte(var1 var2 var3 var4)
"xx" 0 0 1 1
"yy" 1 0 0 9
"zz" 3 2 1 0
end

mata : total = sum(st_data(., "var1 var2 var3 var4")) 

mata : st_numscalar("total", total)

di scalar(total)
18

The two Mata commands could be telescoped.

  • Related