-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettycodebook.ado
More file actions
582 lines (466 loc) · 18.7 KB
/
Copy pathprettycodebook.ado
File metadata and controls
582 lines (466 loc) · 18.7 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
capture program drop prettycodebook
program define prettycodebook
syntax varlist, name(name) datasetname(string) [function(name) statname(string)]
* TO DO: Decide if we want to keep this program name
* TO DO: Take another pass at style sheet; also, delete items that are just
* carryover from markstat and aren't used.
* TO DO: Create help file, toc, and pkg file.
* TO DO (long term): Probably want to add an option so that someone can use their own
* style sheet if they want (or add to existing style sheet)
* Could also have a default page name, though that seems trickier looking
* at the documentation for syntax.
* TO DO: There's quite a lot of repetition across subprograms; these could
* probably be written into functions to make everything more readable.
* NOTE: some additional TODOs are written throughout file.
* Create codebook folder at root of wd, if it doesn't already exist
cap mkdir Codebook
cap mkdir "Codebook/Files"
* Create markdown file
tempname myfile
file open `myfile' using "Codebook/`name'.md", write replace
* Copy in style sheet from GitHub
copy "https://raw.githubusercontent.com/imaddowzimet/Misc/master/codebook.css" "Codebook/Files/codebook.css", replace
* Link to style sheet
file write `myfile' `"<link rel="stylesheet" href="Files/codebook.css"> "' _n
* Write out header info
* TO DO: Make the description field editable as part of the syntax (but keep current text as default option if not specified)
file write `myfile' `"<div class="header"> "' _n
file write `myfile' `"<h1>`datasetname'</h1> "' _n
file write `myfile' `"<div class = "description"> This is the online codebook for `datasetname'. Click any variable for more details, or filter to a selection of variables. </div> "' _n
file write `myfile' `"</div> "' _n
* Javascript for search (source: https://www.w3schools.com/howto/howto_js_filter_lists.asp)
file write `myfile' `"<script> "' _n
file write `myfile' `"function myFunction() { "' _n
file write `myfile' `" var input, filter, ul, li, a, i, txtValue; "' _n
file write `myfile' `" input = document.getElementById('myInput'); "' _n
file write `myfile' `" filter = input.value.toUpperCase(); "' _n
file write `myfile' `" ul = document.getElementById("myUL"); "' _n
file write `myfile' `" li = ul.getElementsByTagName('li'); "' _n
file write `myfile' `" for (i = 0; i < li.length; i++) { "' _n
file write `myfile' `" a = li[i].getElementsByTagName("a")[0]; "' _n
file write `myfile' `" txtValue = a.textContent || a.innerText; "' _n
file write `myfile' `" if (txtValue.toUpperCase().indexOf(filter) > -1) { "' _n
file write `myfile' `" li[i].style.display = ""; "' _n
file write `myfile' `" } else { "' _n
file write `myfile' `" li[i].style.display = "none"; "' _n
file write `myfile' `" } "' _n
file write `myfile' `" } "' _n
file write `myfile' `"} "' _n
file write `myfile' `"</script> "' _n
* Search bar
file write `myfile' `"<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for variables"> "' _n
file write `myfile' "" _n
file write `myfile' "" _n
* Header
file write `myfile' "## List of variables in dataset: " _n
* Start of list that can be filtered by search bar
file write `myfile' `"<ul id="myUL">"' _n
* Create link for a page with all variables detailed info in one
file write `myfile' `"<li><a href="Files/allvars.html"><strong>All Variables</strong>: See all variable details on one page</a></li>"' _n
* Record names of codebook sections, if any are in the [section] characteristic
ds `varlist', has(char section)
local sectionvars `r(varlist)'
if !missing("`sectionvars'") {
local sectionlist
foreach myvar of varlist `sectionvars' {
local sectionname: char `myvar'[section]
if !regexm(`"`sectionlist'"', `"`sectionname'"') {
local sectionlist `"`sectionlist' "`sectionname'""'
}
}
}
* TO DO: Note that the code above breaks if the section names have parentheses;
* so we either need to catch that earlier or program in a fix.
* If it does have section names, write out variables in section blocks
if !missing(`"`sectionlist'"') {
local hassections yes
foreach mysection of local sectionlist {
file write `myfile' "### `mysection': " _n
foreach myvar of varlist `sectionvars' {
di "`myvar'"
local varlabel: variable label `myvar'
local sectionlabel: char `myvar'[section]
if "`sectionlabel'" == "`mysection'" {
file write `myfile' `"<li><a href="Files/`myvar'.html"><strong>`myvar'</strong>: `varlabel'</a></li>"' _n
if missing("`function'") {
qui htmlpagevar `myvar'
}
if !missing("`function'") {
qui htmlpagevar `myvar', function(`function') statname("`statname'")
}
}
}
}
}
* For any variables not in section blocks, put them in a generic section
* (this only gets a label if there are any sections)
ds `varlist', not(char section)
local nonsectionvars `r(varlist)'
if "`hassections'" == "yes" {
file write `myfile' "### Variables not assigned to a section: " _n
}
foreach myvar of varlist `nonsectionvars' {
local varlabel: variable label `myvar'
file write `myfile' `"<li><a href="Files/`myvar'.html"><strong>`myvar'</strong>: `varlabel'</a></li>"' _n
if missing("`function'") {
qui htmlpagevar `myvar'
}
if !missing("`function'") {
qui htmlpagevar `myvar', function(`function') statname("`statname'")
}
}
* End of list that can be operated on by search bar
file write `myfile' `"</ul>"' _n
* Save page
file close `myfile'
* Write out page that lists all variables (this doesn't inherit the sections,
* which I think is OK, but we could see if there's a way to do that)
if missing("`function'") {
qui htmlpageallvars `varlist'
}
if !missing("`function'") {
qui htmlpageallvars `varlist', function(`function') statname("`statname'")
}
* Convert to html
whereis pandoc
local pandoc = r(pandoc)
shell "`pandoc'" "Codebook/`name'.md" -f markdown -t html -s -o "Codebook/`name'.html"
cap erase "Codebook/`name'.md"
* Open page in browser
if c(os) == "MacOSX" view browse "file://`c(pwd)'/Codebook/`name'.html"
if c(os) != "MacOSX" view browse "`c(pwd)'/Codebook/`name'.html"
end
*******************************************************
* Function for writing out specific pages: htmlpagevar
*******************************************************
capture program drop htmlpagevar
program define htmlpagevar
syntax varname [, function(name) statname(string)]
* Create codebook folder at root of wd, if it doesn't already exist
cap mkdir Codebook
cap mkdir "Codebook/Files"
* Create markdown file
tempname myfile
file open `myfile' using "Codebook/Files/`varlist'.md", write replace
* copy in style sheet if it's not already in folder
cap confirm file "Codebook/Files/codebook_page.css"
if _rc copy "https://raw.githubusercontent.com/imaddowzimet/Misc/master/codebook_page.css" "Codebook/Files/codebook_page.css", replace
* link to style sheet
file write `myfile' `"<link rel="stylesheet" href="codebook_page.css"> "' _n
* TO DO: Would probably want to add some code here essentially testing what
* kind of function the user provided - if its only applicable to continous for
* example, and sets locals that can be used to make subsequent code more
* flexible.
* TO DO: Also, would probably be easy to allow a list of custom functions
* instead, though this might be going too deep down the rabbit hole.
* Store variable attributes
* Is it string?
local type: type `varlist'
if regexm("`type'", "str") {
local string yes
}
else {
local string no
}
* Is it categorical?
local categorical: char `varlist'[categorical]
local categorical `=lower("`categorical'")'
* Store value label, if it has one
local vallabel: value label `varlist'
* Store variable label, if it has one
local varlabel : var label `varlist'
* Store variable universe
local myuniverse: char `varlist'[universe]
* For variables not explicitly string or categorical, try to guess whether
* the variable is categorical. // TO DO: Add an option about whether code should do this.
if "`string'" == "no" & "`categorical'" != "yes" {
* Can it be compressed to an integer storage format (byte, int or long)?
tempvar clone
clonevar `clone' = `varlist'
compress `clone'
local type: type `clone'
if inlist("`type'", "byte", "long", "int" ) local integers yes
* How many levels does it have (only relevant if above is true)?
if "`integers'" == "yes" {
levelsof `varlist'
local numvalues: word count `r(levels)'
}
* If it has a value label, then check whether it has <30 values; if it does
* it is probably categorical (or would be amenable to being shown as such)
if "`vallabel'"!="" & "`integers'" == "yes" {
if `numvalues'<30 local categorical yes
}
* if it doesn't, then check whether it has <=5 values; if it does
* it is probably categorical (or would be amenable to being shown as such)
if "`vallabel'"=="" & "`integers'" == "yes" {
if `numvalues'<=5 local categorical yes
}
* To do: this code could probably be improved with real user testing
* Maybe looking to see what proportion of values have real value levels?
}
local myuniverse: char `varlist'[universe]
* Write out general header information
file write `myfile' "**Variable Name:** `varlist' " _n
file write `myfile' "**Variable Label:** `varlabel' " _n
file write `myfile' "**Variable Universe:** `myuniverse' " _n
file write `myfile' " " _n
* If the variable is string, then list all values
if "`string'" == "yes" {
if missing("`function'") {
file write `myfile' "| `varlist' | N | " _n
file write `myfile' "|:----------|----:| " _n
}
if !missing("`function'") {
file write `myfile' "| `varlist' | N | `statname' | " _n
file write `myfile' "|:----------|----:|----:| " _n
}
qui tab `varlist', matcell(values)
qui levelsof `varlist'
local levels r(levels)
local levels `"`r(levels)'"'
local upper: word count `levels'
tokenize `"`levels'"'
foreach mynum of numlist 1/`upper' {
local myvalue = values[`mynum',1]
local mylabel ``mynum''
if missing("`function'") {
file write `myfile' "| `mylabel' | `myvalue'" _n
}
if !missing("`function'") {
`function' `varlist' if `varlist' == "`mylabel'"
file write `myfile' "| `mylabel' | `myvalue' | `r(stat)'" _n
}
}
}
if "`string'" == "no" & "`categorical'" == "yes" {
if missing("`function'") {
file write `myfile' "| `varlist' | value | N | " _n
file write `myfile' "|:----------|-----:|-----:| " _n
}
if !missing("`function'") {
file write `myfile' "| `varlist' | value | N | `statname' | " _n
file write `myfile' "|:----------|-----:|-----:|-----:| " _n
}
qui tab `varlist', matcell(values) missing
qui levelsof `varlist', missing
local levels r(levels)
local levels `"`r(levels)'"'
local upper: word count `levels'
tokenize `"`levels'"'
foreach mynum of numlist 1/`upper' {
local myvalue = values[`mynum',1]
if "`vallabel'" != "" local mylabel: label `vallabel' ``mynum'' // note the extra quotes for mynum, which call the tokenize locals, not mynum itself
if missing("`function'") {
file write `myfile' "| `mylabel' | ``mynum'' | `myvalue'" _n
}
if !missing("`function'") {
`function' `varlist' if `varlist' == ``mynum''
file write `myfile' "| `mylabel' | ``mynum'' | `myvalue' | `r(stat)'" _n
}
}
* Note that when there are 0 observations for a category, it's omitted.
* TO DO: think about whether this is desired behavior.
}
if "`string'" == "no" & "`categorical'" != "yes" {
if missing("`function'") {
file write `myfile' "| Obs | Mean| SD | Min | Max | " _n
file write `myfile' "|-----:|-----:|-----:|-----:|-----:| " _n
}
if !missing("`function'") {
file write `myfile' "| Obs | Mean| SD | Min | Max | `statname' | " _n
file write `myfile' "|-----:|-----:|-----:|-----:|-----:|-----:| " _n
}
qui summ `varlist'
local obs = r(N)
local mean = round(`r(mean)', .001)
local mean: display `mean' %4.3f
local sd = round(`r(sd)' , .001)
local sd: display `sd' %4.3f
local min = round(`r(min)' , .001)
local min: display `min' %4.3f
local max = round(`r(max)' , .001)
local max: display `max' %4.3f
if missing("`function'") {
file write `myfile' "| `obs' | `mean' | `sd' | `min' | `max' | " _n
}
if !missing("`function'") {
`function' `varlist'
file write `myfile' "| `obs' | `mean' | `sd' | `min' | `max' | `r(stat)' | " _n
}
file write `myfile' " " _n
file write `myfile' " " _n
set graphics off
histogram `varlist', scheme(s1mono)
graph export "Codebook/Files/`varlist'.png", width(600) replace
set graphics on
file write `myfile' " "
}
else {
}
file close `myfile'
* Erase html file if it already exists
cap erase "Codebook/Files/`varlist'.html"
whereis pandoc
local pandoc = r(pandoc)
shell "`pandoc'" "Codebook/Files/`varlist'.md" -f markdown -t html -s -o "Codebook/Files/`varlist'.html"
cap erase "Codebook/Files/`varlist'.md"
end
**************************************
** Program to write out all variables
**************************************
capture program drop htmlpageallvars
program define htmlpageallvars
syntax varlist [, function(name) statname(string)]
* Create markdown file
tempname myfile
file open `myfile' using "Codebook/Files/allvars.md", write replace
* link to style sheet
file write `myfile' `"<link rel="stylesheet" href="codebook_page.css"> "' _n
foreach myvar of varlist `varlist' {
* Store variable attributes
* Is it string?
local type: type `myvar'
if regexm("`type'", "str") {
local string yes
}
else {
local string no
}
* Is it categorical?
local categorical: char `myvar'[categorical]
local categorical `=lower("`categorical'")'
* Store value label, if it has one
local vallabel: value label `myvar'
local varlabel: variable label `myvar'
* For variables not explicitly string or categorical, try to guess whether
* the variable is categorical. // TO DO: Add an option about whether code should do this.
if "`string'" == "no" & "`categorical'" != "yes" {
* Can it be compressed to an integer storage format (byte, int or long)?
tempvar clone
clonevar `clone' = `myvar'
compress `clone'
local type: type `clone'
if inlist("`type'", "byte", "long", "int" ) local integers yes
* How many levels does it have (only relevant if above is true)?
if "`integers'" == "yes" {
levelsof `myvar'
local numvalues: word count `r(levels)'
}
* If it has a value label, then check whether it has <30 values; if it does
* it is probably categorical (or would be amenable to being shown as such)
if "`vallabel'"!="" & "`integers'" == "yes" {
if `numvalues'<30 local categorical yes
}
* if it doesn't, then check whether it has <=5 values; if it does
* it is probably categorical (or would be amenable to being shown as such)
if "`vallabel'"=="" & "`integers'" == "yes" {
if `numvalues'<=5 local categorical yes
}
* To do: this code could probably be improved with real user testing
* Maybe looking to see what proportion of values have real value levels?
}
* Store variable universe
local myuniverse: char `myvar'[universe]
* Write out general header information
file write `myfile' "**Variable Name:** `myvar' " _n
file write `myfile' "**Variable Label:** `varlabel' " _n
file write `myfile' "**Variable Universe:** `myuniverse' " _n
file write `myfile' " " _n
* If the variable is string, then list all values
if "`string'" == "yes" {
if missing("`function'") {
file write `myfile' "| `myvar' | N | " _n
file write `myfile' "|:----------|----:| " _n
}
if !missing("`function'") {
file write `myfile' "| `myvar' | N | `statname' | " _n
file write `myfile' "|:----------|----:|----:| " _n
}
qui tab `myvar', matcell(values)
qui levelsof `myvar'
local levels r(levels)
local levels `"`r(levels)'"'
local upper: word count `levels'
tokenize `"`levels'"'
foreach mynum of numlist 1/`upper' {
local myvalue = values[`mynum',1]
local mylabel ``mynum''
if missing("`function'") {
file write `myfile' "| `mylabel' | `myvalue'" _n
}
if !missing("`function'") {
`function' `myvar' if `myvar' == "`mylabel'"
file write `myfile' "| `mylabel' | `myvalue' | `r(stat)'" _n
}
}
}
if "`string'" == "no" & "`categorical'" == "yes" {
if missing("`function'") {
file write `myfile' "| `myvar' | value | N | " _n
file write `myfile' "|:----------|-----:|-----:| " _n
}
if !missing("`function'") {
file write `myfile' "| `myvar' | value | N | `statname' | " _n
file write `myfile' "|:----------|-----:|-----:|-----:| " _n
}
qui tab `myvar', matcell(values) missing
qui levelsof `myvar', missing
local levels r(levels)
local levels `"`r(levels)'"'
local upper: word count `levels'
tokenize `"`levels'"'
foreach mynum of numlist 1/`upper' {
local myvalue = values[`mynum',1]
if "`vallabel'" != "" local mylabel: label `vallabel' ``mynum''
if missing("`function'") {
file write `myfile' "| `mylabel' | ``mynum'' | `myvalue'" _n
}
if !missing("`function'") {
`function' `myvar' if `myvar' == ``mynum''
file write `myfile' "| `mylabel' | ``mynum'' | `myvalue' | `r(stat)'" _n
}
}
}
if "`string'" == "no" & "`categorical'" != "yes" {
if missing("`function'") {
file write `myfile' "| Obs | Mean| SD | Min | Max | " _n
file write `myfile' "|-----:|-----:|-----:|-----:|-----:| " _n
}
if !missing("`function'") {
file write `myfile' "| Obs | Mean| SD | Min | Max | `statname' | " _n
file write `myfile' "|-----:|-----:|-----:|-----:|-----:|-----:| " _n
}
qui summ `myvar'
local obs = r(N)
local mean = round(`r(mean)', .001)
local mean: display `mean' %4.3f
local sd = round(`r(sd)' , .001)
local sd: display `sd' %4.3f
local min = round(`r(min)' , .001)
local min: display `min' %4.3f
local max = round(`r(max)' , .001)
local max: display `max' %4.3f
if missing("`function'") {
file write `myfile' "| `obs' | `mean' | `sd' | `min' | `max' | " _n
}
if !missing("`function'") {
`function' `myvar'
file write `myfile' "| `obs' | `mean' | `sd' | `min' | `max' | `r(stat)' | " _n
}
file write `myfile' " " _n
file write `myfile' " " _n
file write `myfile' " " _n
}
else {
}
file write `myfile' "<br> <br> " _n
file write `myfile' "<hr/> " _n
}
file close `myfile'
* Erase html file if it already exists
cap erase "Codebook/Files/allvars.html"
whereis pandoc
local pandoc = r(pandoc)
shell "`pandoc'" "Codebook/Files/allvars.md" -f markdown -t html -s -o "Codebook/Files/allvars.html"
cap erase "Codebook/Files/allvars.md"
end