-
Notifications
You must be signed in to change notification settings - Fork 0
/
putexcel - excel.do
104 lines (66 loc) · 2.44 KB
/
putexcel - excel.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
sysuse census.dta, clear
//change this file path to your own file path
global output "/Users/linglingpeng/Downloads/FreshEBT/Lingling/blog - output"
*** Excel table
putexcel set "$output/example.xls", replace
//set up the format of Excel table
putexcel A1 = "Title of tables: "
putexcel B2 = "Number of observations"
putexcel C2 = "Mean"
putexcel D2 = "Number of Death"
putexcel E2 = "P-value"
//store all variable names (only use labels)
local varlist marriage pop popurban divorce
local numexcel = 3
foreach X of varlist `varlist' {
local x : variable label `X'
putexcel A`numexcel' = "`x'"
local numexcel = `numexcel' + 3
}
//run a random regression
reg death marriage pop popurban divorce
mat reg_table = r(table)
//if you run "return list", you can see that stata stores the regression above to r(table)
//store all coefficients and p-value in Matrix A
matselrc reg_table A, r(1 4) c(1/4) //select only No.1 and No.4 row and only column 1 to 4
local length = 4 //length of varlist
local rownum = 3 //where do you want to start in Excel spreadsheet? I want to start from row 3
forvalues num = 1/`length' {
local coef = A[1,`num']
local coef_val = round(`coef', 0.001) //keep only three decimal places
local p_val = A[2,`num']
local p_val = round(`p_val', 0.001)
//hard code * into excel spreadsheet
if `p_val' <0.01 {
local coef_val = "`coef_val''"+"***"
}
else if `p_val' <0.05 {
local coef_val = "`coef_val''"+"**"
}
else if `p_val' <0.10 {
local coef_val = "`coef_val'"+"*"
}
putexcel D`rownum' = "`coef_val'" //remember to add the ""
putexcel E`rownum' = `p_val'
local num = `num' + 1
local rownum = `rownum' +3
}
//store all standard errors in Matrix B
matselrc reg_table B, r(2) c(1/4) //select only the second row and only column 1 to 4
local se_row = 4 //we have to start from one row below coefficient. In my excel spreadsheet, it's row 34
forvalues num = 1/`length' {
local se = B[1,`num']
local se_val = round(`se', 0.001) //keep only three decimal places
local se = string(`se_val',"%8.3f") //turn it into string before put in on Excel
putexcel D`se_row' = "(`se')" //add () around standard error
local num = `num' + 1
local se_row = `se_row' +3
}
//use sum command to store number of observations and mean
local sumnum = 3
foreach var in `varlist' {
sum `var'
putexcel B`sumnum'= (r(N))
putexcel C`sumnum' = (r(mean))
local sumnum = `sumnum' + 3
}