-
Notifications
You must be signed in to change notification settings - Fork 17
/
edabar.ado
executable file
·225 lines (156 loc) · 7.4 KB
/
edabar.ado
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
********************************************************************************
* Description of the Program - *
* EDA subroutine used to create bar graphs *
* *
* Program Output - *
* Creates bar graph GPH and PDF as well as entries in the LaTeX document *
* *
* Lines - *
* 224 *
* *
********************************************************************************
*! edabar
*! v 0.0.1
*! 17jul2018
// Drop program from memory if already loaded
cap prog drop edabar
// Define program
prog def edabar
// Version used to interpret code
version 14
// Syntax structure for edabar subroutine
syntax varlist(min=1) [if] [in], BARType(string asis) ///
root(string asis) ///
[BARGRAphopts(string asis) ///
scheme(passthru) ///
keepgph MISSing byvars(varlist) byseq ]
// Mark only the observations to use
marksample touse, strok novarlist
// If the byvars parameter has no arguments
if "`byvars'" == "" {
// Add subsubsection header for categorical data
file write doc "\subsubsection{Bar Graphs} \newpage\clearpage" _n
// Create bar graphs for the categorical variables
foreach v of var `varlist' {
// Define a macro used to set legend parameters
loc leg legend(rows(`:char `v'[lrows]') symy(1.85) symx(1.85))
// Create the histogram
gr bar (`bartype') if `touse', over(`v') asyvars `bargraphopts' ///
`scheme' ti(`: char `v'[title]') `leg' `missing' ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export to .pdf file
qui: gr export `"`root'/graphs/bar`v'.pdf"', as(pdf) replace
// Get prepped version of the variable name
texclean `"`v'"', r
// Store variable name in local macro ref
loc ref `r(clntex)'
// Get the cleaned variable label
texclean `"`: var l `v''"'
// Store the cleaned variable label
loc cap `r(clntex)'
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{`cap' \label{fig:bar`v'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{bar`v'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
// Check if user wants to keep the GPH files
if "`keepgph'" != "" {
// Define local macro with syntax to remove file
qui: gr save `"`root'/graphs/bar`v'.gph"', replace
} // End IF Block to remove .gph files
} // End loop over categorical variables
} // End IF Block for single variable
// If the byvars parameter has arguments and user specifies sequentially
else if "`byvars'" != "" & "`byseq'" != "" {
// Create bar graphs for the categorical variables
foreach v of var `varlist' {
// Loop over the bygroup variables
foreach b of var `byvars' {
// Get name of the by variable
texclean `"`b'"', r
// Store by variable name in bref
loc bref `r(clntex)'
// Get by variable label
texclean `"`: var l `b''"'
// Store label
loc blab `r(clntex)'
// Get the name of the bar variable
texclean `"`v'"', r
// Store bar variable name
loc ref `r(clntex)'
// Combined Label for Captions
texclean `"`: var l `v'' by `blab'"'
// Store the caption string
loc cap `r(clntex)'
// Double check that all internal quotation marks are removed
loc cap : subinstr loc cap `"""' "", all
// Add subsubsection header for categorical data
file write doc "\subsubsection{Bar Graphs by `blab'} \newpage\clearpage" _n
// Define a macro used to set legend parameters
loc leg legend(rows(`:char `v'[lrows]') symy(1.85) symx(1.85))
// Create the histogram
cap gr bar (`bartype') if `touse', over(`v') asyvars ///
`bargraphopts' `scheme' `missing' by(`b', `leg' ///
ti(`: char `v'[title]') ///
note("Created on: `c(current_date)' at: `c(current_time)'"))
// Check return code
if _rc == 0 {
// Export to .pdf file
qui: gr export `"`root'/graphs/bar`v'By`bref'.pdf"', as(pdf) replace
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{`cap' \label{fig:bar`v'By`bref'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{bar`v'By`bref'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
// Check if user wants to keep the GPH files
if "`keepgph'" != "" {
// Define local macro with syntax to remove file
qui: gr save `"`root'/graphs/bar`v'By`bref'.gph"', replace
} // End IF Block to remove .gph files
} // End IF Block for return code check
} // End Loop over by variables
} // End loop over categorical variables
} // End ELSEIF Block for sequential by graphs
// If the byvars parameter has arguments and user specifies sequentially
else if "`byvars'" != "" & "`byseq'" == "" {
// Create bar graphs for the categorical variables
foreach v of var `varlist' {
// Get the name of the bar variable
texclean `"`v'"', r
// Store bar variable name
loc ref `r(clntex)'
// Comma separate the byvars
loc blab `: subinstr loc byvars `" "' `", "', all'
// Combined Label for Captions
texclean `"`: var l `v'' by `blab'"'
// Store the caption string
loc cap `r(clntex)'
// Double check that all internal quotation marks are removed
loc cap : subinstr loc cap `"""' "", all
// Add subsubsection header for categorical data
file write doc "\subsubsection{Bar Graphs by `blab'} \newpage\clearpage" _n
// Define a macro used to set legend parameters
loc leg legend(rows(`:char `v'[lrows]') symy(1.85) symx(1.85))
// Create the histogram
cap gr bar (`bartype') if `touse', over(`v') asyvars `missing' ///
`bargraphopts' `scheme' by(`byvars', ti(`: char `v'[title]') ///
note("Created on: `c(current_date)' at: `c(current_time)'") `leg')
// Check return code
if _rc == 0 {
// Export to .pdf file
qui: gr export `"`root'/graphs/bar`v'ByGraph.pdf"', as(pdf) replace
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{`cap' \label{fig:bar`v'ByGraph}}"' _n
file write doc `"\includegraphics[width=\textwidth]{bar`v'ByGraph.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
// Check if user wants to keep the GPH files
if "`keepgph'" != "" {
// Define local macro with syntax to remove file
qui: gr save `"`root'/graphs/bar`v'ByGraph.gph"', replace
} // End IF Block to remove .gph files
} // End IF Block for successful return code
} // End loop over categorical variables
} // End ELSE Block for by graphs
// End Subroutine definition
end