/
datadoc.ado
290 lines (228 loc) · 7.93 KB
/
datadoc.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/***
_v. 1.4_
datadoc
=======
generates data documentation template based on CRAN's layout.
Syntax
------
> __datadoc__ [, replace]
### Options
- _replace_ option replaces the existing file |
Description
-----------
the output includes a do-file documentation and its Stata help file.
the command requires a data to be loaded in the memory.
If not, a template is generated.
Example
-------
generate _example.md_ documentation layout to be filled manually
. clear
. datadoc
generate a data documentation template for __auto.dta__
. sysuse auto, clear
. datadoc
add notes to the dataset and variables
. sysuse auto, clear
. notes : this dataset is included in Stata 15
. notes : for more information see github.com/haghish/datadoc
. notes price: this is the price of the car
. notes make: add information about the make variable
. notes weight: explain ...
. datadoc, replace
if a variable includes more than one note, __datadoc__ will present the
informattion in a different format:
. notes price: this is the second note of the price variable
. datadoc, replace
Author
------
E. F. Haghish
_haghish@med.uni-goesttingen.de_
[https://github.com/haghish/echo](https://github.com/haghish/echo)
License
-------
MIT License
- - -
This help file was dynamically produced by
[MarkDoc Literate Programming package](http://www.haghish.com/markdoc/)
***/
*cap prog drop datadoc
program define datadoc
version 15
syntax [anything] ///
[, ///
replace /// replaces the current sthlp file, if it already exists
]
if "`c(filename)'" == "" {
di as err "no data is loaded in the memory, an empty template is generated called {bf:example.do}"
local name "example"
local script example.do
}
else {
qui abspath "`c(filename)'"
local name "`r(fname)'"
local name : subinstr local name ".dta" ""
local script `name'.do
}
// -------------------------------------------------------------------------
// Part 1: Adding the template
// =========================================================================
tempfile tmp
tempname knot
qui file open `knot' using `"`tmp'"', write replace
capture abspath "`c(filename)'"
local dataname "`r(fname)'"
if "`dataname'" == "" local dataname "YYY"
local nvar "`c(k)'"
if "`nvar'" == "" local nvar "???"
local nobs "`c(N)'"
if "`nobs'" == "" local nobs "???"
local len = length("`dataname'") + 8
// get the label of the data
qui describe
local datalabel "`r(datalabel)'"
if "`datalabel'" == "" local datalabel "label of the dataset"
file write `knot' ///
"/***" _n ///
"`dataname'" _n ///
_dup(`len') "=" _n(2) ///
"`datalabel' ... included in XXX package" _n(2) ///
"Description" _n ///
"----------- " _n(2) ///
"The __`dataname'__ dataset is about ... " _n(2) ///
"Format" _n ///
"------ " _n(2) ///
"__`dataname'__ dataset includes _`nobs'_ observations and _`nvar'_ variables." _n(2) ///
"### Summary of the variables" _n(2)
if "`c(filename)'" == "" {
file write `knot' ///
"| _Variable_ | _Type_ | _Description_ |" _n ///
"|:------------|:---------|:-----------------------|" _n ///
"| __var1__ | numeric | explain var1 |" _n ///
"| __var2__ | string | explain var2 |" _n(2)
}
else {
// 15 character for varname, 5 character for type, 60 for description
// get the longest label and check for notes
local maxlength 0
foreach var of varlist _all {
local lab: variable label `var'
local lablen = length("`lab'")
if `lablen' > `maxlength' local maxlength `lablen'
if "``var'[note1]'" != "" local varnotes 1 // define varnotes
if "``var'[note2]'" != "" {
local secondnote 1 // define varnotes
}
}
file write `knot' ///
"| Variable | Type | Description "
if `maxlength' > 58 {
file write `knot' _dup(45) " " "|" _n
}
else if `maxlength' > 25 {
local endpoint1 = `maxlength'-12
file write `knot' _dup(`endpoint1') " " "|" _n
}
else {
file write `knot' _dup(13) " " "|" _n
}
file write `knot' "|:--------------|:-------|:"
if `maxlength' > 58 {
file write `knot' "---------------------------------------------------------|" _n
}
else if `maxlength' > 25 {
file write `knot' _dup(`maxlength') "-" "|" _n
}
else {
file write `knot' _dup(25) "-" "|" _n
}
foreach var of varlist _all {
local vartype: type `var'
local lab: variable label `var'
local lab = substr("`lab'",1,57)
//if substr("`vartype'",1,3)=="str" local vartype "str"
//else if substr("`vartype'",1,5)=="float" local vartype "flt"
//else if substr("`vartype'",1,4)=="byte" local vartype "byt"
local varname = abbrev("`var'",12)
file write `knot' "| `varname'" _col(16) " | `vartype'" _col(25) " | `lab'" //_col(83) "|" _n
if `maxlength' > 58 {
file write `knot' _col(83) "|" _n
}
else if `maxlength' > 25 {
local endpoint = `maxlength'+26
file write `knot' _col(`endpoint') "|" _n
}
else {
file write `knot' _col(51) "|" _n
}
}
file write `knot' _n
}
file write `knot' ///
"Notes" _n ///
"----- " _n(2) ///
"### Dataset" _n(2)
if "`_dta[note1]'" != "" {
local notenum 1
while "`_dta[note`notenum']'" != "" {
file write `knot' "`notenum'. `_dta[note`notenum']'" _n
local notenum = `notenum'+1
}
}
else {
file write `knot' "The dataset doesn't include any note" _n
}
file write `knot' _n
// variable notes
file write `knot' _n "### Variables" _n(2)
if missing("`varnotes'") file write `knot' "The variables don't include any note" _n(2)
else {
if !missing("`secondnote'") {
foreach var of varlist _all {
if "``var'[note1]'" != "" {
file write `knot' "#### `var'" _n(2)
local notenum 1
while "``var'[note`notenum']'" != "" {
file write `knot' "`notenum'. ``var'[note`notenum']'" _n
local notenum = `notenum'+1
}
file write `knot' _n(2)
}
}
}
else {
file write `knot' ///
"| _Variable_ | _Note_ |" _n ///
"|:-------------|:-------------------------------------------|" _n
foreach var of varlist _all {
if "``var'[note1]'" != "" {
local varname = abbrev("`var'",12)
local varnote = substr("``var'[note1]'",1,43)
file write `knot' "| `varname'" _col(16) "| `varnote'" _col(60) " |" _n
}
}
file write `knot' _n
}
}
file write `knot' ///
"Source" _n ///
"------ " _n(2) ///
"Cite the source ..." _n(2) ///
"References" _n ///
"---------- " _n(2) ///
"Cite the references ..." _n ///
"***/" _n(2)
file close `knot'
capture copy "`tmp'" "`script'", replace public
confirm file "`script'"
cap findfile sthlp.ado
if _rc == 0 {
qui sthlp `script', export(sthlp) `replace'
if _rc == 0 {
di as txt "{p}(MarkDoc created "`"{bf:{browse "`script'"}} and {bf:{browse "`name'.sthlp"}})"'
}
}
else {
di as txt "{p}(datadoc created "`"{bf:{browse "`script'"}})"' _n
di as err `"install {browse "https://github.com/haghish/markdoc":markdoc} package to generate the Stata help file from a script file"'
}
end