Export average value of one variable over 2 other variables

89 Views Asked by At

I can use tabout var1 var2...style(csv) to export a matrix-like object with var1 on one axis, var2 on the other, and the frequencies that each combination of the values var1 and var2 occur. Is it possible to use tabout or some other commmand (preferably native to Stata) to do something similar except instead of the frequencies filling the cells, the average value of a third variable would. For example, suppose the skeleton of this structure is as follows:

    A    B    C    D
1
2
3

var2 takes on values of 1, 2, 3 and var1 takes on values A, B, C, D. Suppose that there is a third variable, var3 that is continuous. Suppose the average value of var3 when var1==A and var2==1 is 3.1. Then this cell would get filled in:

    A    B    C    D
1  3.1
2
3

The other values would similarly be filled in. Is there any way to do this in Stata?

1

There are 1 best solutions below

1
On BEST ANSWER

You can use table's contents() option:

clear
set more off

sysuse auto

table foreign rep78, contents(mean price)

Something similar can be achieved with tabout (from SSC):

tabout foreign rep78 using testtab.csv, sum cells(mean price) ptotal(none)