/
fs_strings.do
154 lines (127 loc) · 4.7 KB
/
fs_strings.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
cls
clear
macro drop _all
/*
Back to Fan's Stata4Econ or other repositories:
- http://fanwangecon.github.io
- http://fanwangecon.github.io/Stata4Econ
- http://fanwangecon.github.io/R4Econ
- http://fanwangecon.github.io/REconTools
- http://fanwangecon.github.io/M4Econ
- http://fanwangecon.github.io/Tex4Econ
- http://fanwangecon.github.io/CodeDynaAsset/
- http://fanwangecon.github.io/Math4Econ/
- http://fanwangecon.github.io/Stat4Econ/
1. define variables through delimit
2. define string with quotes
3. run regression, and use defined string as labels for rows in esttab
4. replace all occurances of elements in strings
*/
///--- Start log
set more off
capture log close _all
cd "${root_log}"
global st_link "/prog/define/fs_strings"
global curlogfile "~/Stata4Econ/${st_link}"
global st_logname "stata_fs_strings"
log using "${curlogfile}" , replace name($st_logname)
log on $st_logname
///-- Site Link: Fan's Project Reusable Stata Codes Table of Content
di "https://fanwangecon.github.io/"
di "https://fanwangecon.github.io/Stata4Econ/"
///-- File Title
global filetitle "Stata string delimit, string with quotes, string regression labels, etc."
///////////////////////////////////////////////////////////////////////////////
///--- String Operations
///////////////////////////////////////////////////////////////////////////////
///--- Search and Replace Text in Substring
* replace quote in string
di subinstr(`"dataVar1 " dataVar2"',`"""',"",.)
* Replace quotes in string
di subinstr(`" "dataVar1 dataVar2 " "dataVar2 dataVar3" "',`"""',"",.)
* Replace & with /& in long string
global scd ""
global scd "${scd} Conditions: PA=(& el\_i\_mand\_talk\_m2a != -999 & S\_han !=.);"
global scd "${scd} PB=(& el\_i\_mand\_talk\_m2a != -999 & S\_han == 0);"
global scd "${scd} PC=(& el\_i\_mand\_talk\_m2a != -999 & S\_han == 1);"
global scd "${scd} common=(S\_han !=. & AgeCloseYr\_i\_G1 <= 30 & H\_age <= 44"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd "${scd} & (vE\_schCloseYr\_full >= 1998 | vE\_schCloseYr\_full == 0)"
global scd = subinstr("${scd}","&","\&",.)
di "${scd}"
* Replace dash
local tableRefName = "a_b_c"
local tableRefName = subinstr("`tableRefName'","_","",.)
di "`tableRefName'"
* replace pound
local instrCap = "_d1_l1#_d1_l2 _d2_l2#_d2_l4"
local cinstrCapF = subinstr(word("`instrCap'",1),"#"," ",.)
di "`cinstrCapF'"
///////////////////////////////////////////////////////////////////////////////
///--- String Definitions and Regressions
///////////////////////////////////////////////////////////////////////////////
///--- Load Data
set more off
sysuse auto, clear
///--- Define Multiple Variables as global in delimit
#delimit;
global vars_rhs "
mpg
ib1.rep78
headroom trunk
weight
";
#delimit cr
di `"$vars_rhs"'
///--- Define String with Quotes
#delimit;
global st_coef_label "
mpg "mpg variable"
1.rep78 "BASE GROUP CONSTANT = rep78 is 1"
2.rep78 "rep78 is 2"
3.rep78 "rep78 is 3"
4.rep78 "rep78 is 4"
5.rep78 "rep78 is 5"
headroom "headroom variable"
trunk "this is the trunk variable"
weight "and here the weight variable"
";
#delimit cr
di `"$st_coef_label"'
///--- Describe and Summarize
d $rhs_vars_list, f
summ $rhs_vars_list
///--- Run Regression
eststo clear
eststo, title("reg1"): regress price $vars_rhs if foreign == 0
eststo, title("reg2"): regress price $vars_rhs if foreign == 1
esttab, title("regtest") ///
mtitle ///
coeflabels($st_coef_label) ///
varwidth(50)
///--- End Log and to HTML
log close _all
capture noisily {
log2html "${curlogfile}", replace title($filetitle (<a href="https://github.com/FanWangEcon/Stata4Econ/blob/master${st_link}.do">DO</a>, more see: <a href="https://fanwangecon.github.io/">Fan</a> and <a href="https://fanwangecon.github.io/Stata4Econ">Stata4Econ</a>))
}
///--- to PDF
capture noisily {
translator set Results2pdf logo off
translator set Results2pdf fontsize 10
translator set Results2pdf pagesize custom
translator set Results2pdf pagewidth 8.27
translator set Results2pdf pageheight 11.69
translator set Results2pdf lmargin 0.2
translator set Results2pdf rmargin 0.2
translator set Results2pdf tmargin 0.2
translator set Results2pdf bmargin 0.2
translate @Results "${curlogfile}.pdf", replace translator(Results2pdf)
}
capture noisily {
erase "${curlogfile}.smcl"
}