Hello helpful Stata users
I require (possibly nested?) loop helpt. I have already seen the thread, blog and entries on this, but I need help if possible!
I am required to produce rows of summary statistics for a range of continuous variables by a range of categorical variables within sets of dichotomous variables. I do not need the totals.
e.g. (from Stata dataset auto)
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of price by make
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of weight by make
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of price by make, for each value of rep78
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of weight by make, for each value of rep78
etc.
I have written the following
cd "C:"
Sysuse foreign
putexcel set myresult
putexcel set myresult, replace sheet("Autot1")
local row=2
foreach x of varlist weight length {
tabstat `x', by(foreign) statistics (n min max mean sd med p25 p75) save
matrix Stat2 = r(Stat2)'
matrix Stat1 = r(Stat1)'
putexcel A`row'=("`x'")
putexcel C`row'= matrix(Stat2)
putexcel D`row'= matrix(Stat1)
local ++row
}
The data are displaying correctly in that the summary statistics are in the same row as the name of the variable and each variable is under the next one.
However, the matrices are writing over each other, so only the final row for each variable is displayed in the excel sheet
Last, I do not understand how to set the variables to appear in the by() command. I think I need a nested loop, but I cannot work it out
Many thanks
I require (possibly nested?) loop helpt. I have already seen the thread, blog and entries on this, but I need help if possible!
I am required to produce rows of summary statistics for a range of continuous variables by a range of categorical variables within sets of dichotomous variables. I do not need the totals.
e.g. (from Stata dataset auto)
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of price by make
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of weight by make
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of price by make, for each value of rep78
r(N) r(min) r(max) r(sd) r(p50) r(p10) r(p75) of weight by make, for each value of rep78
etc.
I have written the following
cd "C:"
Sysuse foreign
putexcel set myresult
putexcel set myresult, replace sheet("Autot1")
local row=2
foreach x of varlist weight length {
tabstat `x', by(foreign) statistics (n min max mean sd med p25 p75) save
matrix Stat2 = r(Stat2)'
matrix Stat1 = r(Stat1)'
putexcel A`row'=("`x'")
putexcel C`row'= matrix(Stat2)
putexcel D`row'= matrix(Stat1)
local ++row
}
The data are displaying correctly in that the summary statistics are in the same row as the name of the variable and each variable is under the next one.
However, the matrices are writing over each other, so only the final row for each variable is displayed in the excel sheet
Last, I do not understand how to set the variables to appear in the by() command. I think I need a nested loop, but I cannot work it out
Many thanks
Comment