Repository navigation
Expand file tree
/
Copy pathassertlist_cleanup.ado
More file actions
457 lines (359 loc) · 15.5 KB
/
Copy pathassertlist_cleanup.ado
File metadata and controls
457 lines (359 loc) · 15.5 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
*! assertlist_cleanup version 1.16 - Biostat Global Consulting - 2023-01-29
* This program can be used after assertlist to cleanup the column
* names and make them more user friendly
*******************************************************************************
* Change log
* Updated
* version
* Date number Name What Changed
* 2018-02-08 1.00 MK Trimner Original Version
* 2018-04-10 1.01 MK Trimner Added sorting option and broke into programs
* 2018-05-10 1.02 MK Trimner Corrected typo
* 2018-06-06 1.03 MK Trimner Added code to strip .xls or .xlsx extension
* 2018-09-13 1.04 MK Trimner Added code to destring id variables when possible
* Had to add code to create a temp string variable as well
* in rename program
* 2018-09-27 1.05 MK Trimner Added code to string .xls or .xlsx extension from NAME
* 2018-10-04 1.06 MK Trimner Added a min of 30 for column width to prevent errors with
* column widths that are too long
* Also added txtwrap for entire sheet after all other formatting
* is completed.
* 2018-10-10 1.07 MK Trimner Corrected message
* 2018-10-24 1.08 Dale Rhoda Use numtobase26() to pull the Excel column name we need
* 2018-11-21 1.09 MK Trimner -Removed CONCATENATE formula (Replace command) and put
* in replace program to speed up process.
* - Added code to set column width to 0 if
* replace column or var type
* 2019-02-21 1.10 MK Trimner Cleaned up formatting excel subprogram to remove code
* Also changed it to format each tab rather than each column at a time
* Adjusted column width criteria so it takes into account the difference
* between varname and value lengths rather than if name is greater
* than values
* 2019-04-14 1.11 MK Trimner Removed column for replace statement
* and all code related
* 2020-04-09 1.12 MK Trimner Added code to allow for LIST option with FIX spreadsheets
* changed column width
* Added new names for Assertlist Summary tab to show idlist checklist and list values
* 2020-08-13 1.13 MK Trimner Added code to cleanup ID tab if populated
* Changed syntax check for "fix" tabs
* 2020-09-07 1.14 MK Trimner Added idsort to non-fix tabs as well
* 2020-09-09 MK Trimner Removed Completed from sequence number
* 2021-03-31 1.15 MK Trimner Added noFormat option to allow Stata to run faster and avoid Excel formatting errors due to large size
* 2022-03-14 1.16 MK Trimner Added txtwrap option to the rows in the failed assertion tabs
* 2023-01-31 1.17 MK Trimner Changed the noFORMAT option to be FORMAT. the default will now be to not format.
* Added fmtids when using stata version >= 15
*******************************************************************************
*
* Contact Dale Rhoda (Dale.Rhoda@biostatglobal.com) with comments & suggestions.
*
program define assertlist_cleanup
syntax , EXCEL(string asis) [ NAME(string asis) IDSORT FORMAT]
noi di as text "Confirming excel file exists..."
* If the user specified a .xls or .xlsx extension in NAME or EXCEL, strip it off here
foreach v in excel name {
if lower(substr("``v''",-4,.)) == ".xls" ///
local `v' `=substr("``v''",1,length("``v''")-4)'
if lower(substr("``v''",-5,.)) == ".xlsx" ///
local `v' `=substr("``v''",1,length("``v''")-5)'
}
* Make sure file provided exists
capture confirm file "`excel'.xlsx"
if _rc!=0 {
* If file not found, display error and exit program
noi di as error "Spreadsheet provided in macro EXCEL does not exist." ///
" Current value provided was: `excel'"
noi di as error "Exiting program..."
exit 99
}
else {
* Describe excel file to determine how many sheets are present
capture import excel using "`excel'.xlsx", describe
local f `=r(N_worksheet)'
* If user requests a new file name, create copy and save as NAME
if "`name'"!="" {
noi di as text "Making copy of excel file named: `name'.xlsx..."
copy "`excel'.xlsx" "`name'.xlsx", replace
* Set excel local to new file name
local excel `name'
}
* Go through each of the sheets
forvalues b = 1/`f' {
* Create a local that will hold the length of each header
local passthrough 0
local hide 0
* Bring in the sheet
capture import excel using "`excel'.xlsx", describe
* Capture the sheet name
local sheet `=r(worksheet_`b')'
if "`sheet'" == "List of IDs failed assertions" assertlist_cleanup_id_tab, excel(`excel') sheet(`sheet') `format'
else {
* Import file
noi di as text "Importing excel sheet: `sheet'..."
import excel "`excel'.xlsx", sheet("`sheet'") firstrow clear allstring
* Grab column count
qui describe
local columns = r(k)
local rows = `=`r(N)'+1'
* Create a local with the cell range for sheet
local range `=r(range_`b')'
* Set local for max number of vars checked
local max 0
local start 2
* If it is a fix sheet, sort the variables by id
if lower(substr("`sheet'",-4,.)) == "_fix" {
* Grab the max number of vars checked
qui {
capture confirm var _al_num_var_checked
if _rc==0 {
tempvar num_var_checked_l
destring _al_num_var_checked, gen(`num_var_checked_l')
qui summarize `num_var_checked_l'
local max `=r(max)'
local start 3
drop `num_var_checked_l'
}
}
}
if "`sheet'" != "Assertlist_Summary" & "`idsort'"!="" assertlist_cleanup_idsort, excel(`excel') sheet(`sheet') max(`max') start(`start') `format'
* Remove _al from var names
local n 1
noi di as text "Renaming variables and formatting columns..."
foreach v of varlist * {
* Rename all the variables
assertlist_cleanup_rename, excel(`excel') sheet(`sheet') n(`n') ///
max(`max') var(`v') passthrough(`passthrough') hide(`hide') rows(`rows') `format'
local ++n
}
* Format header row for each tab
if "`format'" != "" assertlist_cleanup_format_header, excel(`excel') sheet(`sheet') ///
passthrough(`passthrough') hide(`hide')
}
}
}
end
********************************************************************************
********************************************************************************
****** Assertlist Cleanup Sort *****
********************************************************************************
********************************************************************************
capture program drop assertlist_cleanup_idsort
program define assertlist_cleanup_idsort
syntax, EXCEL(string asis) SHEET(string asis) MAX(int) START(int) [FORMAT]
noi di as text "Sort sheet by ID Variables..."
qui {
* Double check that IDlist provided is the same as previously used
local e
foreach v of varlist * {
if strpos("`e'","_al_assertion_syntax")==0 {
local e `e' `v'
capture destring `v', replace
}
}
* Determine the number of words in previous IDlist
* Need to subtract 1 as _al_assertion_syntax is included in list
local enum = `= wordcount("`e'") - 1'
* Create local with the idlist
* Start at the 3rd word in `e' as the first two are
* check_sequence and num_var_checked
local elist
forvalues i = `start'/`enum' {
local elist `elist' `=word("`e'",`i')'
}
* sort by the ids found in elist
sort `elist'
* Export the new sorted data
export excel using "`excel'.xlsx", sheet("`sheet'") sheetmodify ///
firstrow(var) nolabel datestring("%tdDD/Mon/CCYY")
}
end
********************************************************************************
********************************************************************************
****** Rename Excel Variables *****
********************************************************************************
********************************************************************************
capture program drop assertlist_cleanup_rename
program define assertlist_cleanup_rename
syntax , EXCEL(string asis) SHEET(string asis) N(int) MAX(int) VAR(varlist) ///
PASSTHROUGH(string asis) HIDE(string asis) ROWS(int) [FORMAT]
qui {
local v `var'
* Reset two locals that will be trigger column width formatting
local hide_var
* Grab the max length for formatting
tempvar `v'_l
if substr("`: type `v''",1,3) != "str" tostring(`v'), replace
gen ``v'_l'=length(`v')
qui summarize ``v'_l'
local m`n'1=`=r(max) + 1'
drop ``v'_l'
local `v' `=subinstr("`v'","_al_","",1)'
* Grab the var name and placement for putexcel purpose
local `v'n `n'
if "``v''"=="check_sequence" local `v' Assertion Sequence Number
if "``v''"=="obs_number" local `v' Observation Number in Dataset
if "``v''"=="assertion_syntax" local `v' Assertion Syntax That Failed
if "``v''"=="tag" local `v' User Specified Additional Information
if "``v''"=="total" {
local `v' Total Number of Observations Included in Assertion
local m`n'1 17
}
if "``v''"=="number_passed" local `v' Number That Passed Assertion
if "``v''"=="number_failed" local `v' Number That Failed Assertion
if "``v''"=="note1" local `v' Note
if "``v''"=="sheet" local `v' Sheet Name That Contains Assertion Output
if "``v''"=="idlist" local `v' Variables Provided in IDLIST Option
if "``v''"=="list" local `v' Variables Provided in LIST Option
if "``v''"=="checklist" local `v' Variables Provided in CHECKLIST Option
if "``v''"=="num_var_checked" local `v' Number of Variables Checked in Assertion
if `max'!=0 {
forvalues i = 1/`max' {
if "``v''"=="var_`i'" local `v' Name of Variable `i' Checked in Assertion
if "``v''"=="var_type_`i'" {
local `v' Value type of Variable `i'
local hide_var yes
}
if "``v''"=="original_var_`i'" local `v' Current Value of Variable `i'
if "``v''"=="correct_var_`i'" {
local `v' Blank Space for User to Provide Correct Value of Variable `i'
local m`n'1 20
}
}
}
* also create local with max of variable name
local m`n'2 =length("``v''")
* Put the new variable name into excel file
putexcel set "`excel'.xlsx", modify sheet("`sheet'")
mata: st_local("xlcolname", invtokens(numtobase26(``v'n')))
if "`format'" != "" {
putexcel `xlcolname'1 = "``v''", txtwrap bold left fpattern("solid", "lightgray")
putexcel `xlcolname'2:`xlcolname'`rows' , txtwrap
}
else putexcel `xlcolname'1 = "``v''"
if "`hide_var'"=="yes" local hide `hide' ``v'n'
*if `n'==1 local passthrough `m`n'2'
local passthrough `passthrough' `m`n'2'
* Pass through the locals
foreach v in passthrough hide {
c_local `v' ``v''
}
}
end
********************************************************************************
********************************************************************************
****** Format Excel Tabs *****
********************************************************************************
********************************************************************************
capture program drop assertlist_cleanup_format_header
program define assertlist_cleanup_format_header
syntax , EXCEL(string asis) SHEET(string asis) PASSTHROUGH(string asis) HIDE(string asis)
* Format the width of each column
* use mata to populate table formatting
qui {
mata: b = xl()
mata: b.load_book("`excel'.xlsx")
mata: b.set_mode("open")
mata: b.set_sheet("`sheet'")
* Determine the column widths
noi import excel using "`excel'.xlsx", sheet("`sheet'") ///
firstrow allstring clear
describe
local m_v=`=r(k)'
local r_v=`=r(N)'
local i 1
foreach v of varlist * {
tempvar `v'_l
gen ``v'_l'=length(`v')
summarize ``v'_l'
local m`i'1=min(`=`r(max)'+1',25)
local m`i'2=word("`passthrough'",`i')
drop ``v'_l'
local ++i
}
if $FORMATTING_VERSION == 15 {
* Create a bold font
mata: bold_font = b.add_fontid()
mata: b.fontid_set_font(bold_font, "Calibri", 11, "black")
mata: b.fontid_set_font_bold(bold_font, "on" )
mata: b.fontid_set_font_italic(bold_font, "off" )
mata assertlist_header = b.add_fmtid()
mata: b.fmtid_set_fontid(assertlist_header, bold_font)
mata: b.fmtid_set_fill_pattern(assertlist_header, "solid","lightgray")
mata: b.fmtid_set_horizontal_align(assertlist_header, "left")
mata: b.fmtid_set_text_wrap(assertlist_header, "on")
mata: b.set_fmtid(1,(1,`m_v'),assertlist_header)
forvalues i = 1/`m_v' {
local width = max(`=`m`i'1'+3',10)
if `m`i'2' - `m`i'1' > 5 local width `=`m`i'1'+ 11'
if `m`i'2' - `m`i'1' > 15 local width `=`m`i'1'+ 14'
*mata: b.set_column_width(`i',`i',`=min(30,`width')')
local cw `=min(30,`width')'
mata format_width_`cw' = b.add_fmtid()
mata: b.fmtid_set_text_wrap(format_width_`cw'', "on")
mata: b.fmtid_set_column_width(format_width_`cw',`i',`i', `cw')
mata: b.set_fmtid(2,`i',format_width_`cw')
}
mata assertlist_all_others = b.add_fmtid()
mata: b.fmtid_set_text_wrap(assertlist_all_others, "on")
mata: b.set_fmtid((3,`=`r_v'+1'),(1,`m_v'),assertlist_all_others)
foreach l in `=substr("`hide'",3,.)' {
mata format_hide_`l' = b.add_fmtid()
mata: b.fmtid_set_column_width(format_hide_`l',`l',`l',0)
mata: b.set_fmtid(`l',`l',format_hide_`l')
}
}
if $FORMATTING_VERSION == 14 {
forvalues i = 1/`m_v' {
* Set column width
local width = max(`=`m`i'1'+3',10)
if `m`i'2' - `m`i'1' > 5 local width `=`m`i'1'+ 11'
if `m`i'2' - `m`i'1' > 15 local width `=`m`i'1'+ 14'
mata: b.set_column_width(`i',`i',`=min(30,`width')')
}
foreach l in `=substr("`hide'",3,.)' {
mata: b.set_column_width(`l',`l',0)
}
}
* Set the row height
mata: b.set_row_height(1,1,80)
mata b.close_book()
}
end
********************************************************************************
********************************************************************************
****** Rename Excel Variables *****
********************************************************************************
********************************************************************************
capture program drop assertlist_cleanup_id_tab
program define assertlist_cleanup_id_tab
syntax , EXCEL(string asis) SHEET(string asis) [FORMAT]
qui {
* Import file
noi di as text "Importing excel sheet: `sheet'..."
import excel "`excel'.xlsx", sheet("`sheet'") firstrow clear allstring
destring _al_number_assertions_failed, replace
summarize _al_number_assertions_failed
local max = r(max)
foreach v of varlist* {
local `v' `v'
if "`v'" == "_al_idlist" local _al_idlist List of Variables Used to Identify Line in Assertion
if "`v'" == "_al_number_assertions_failed" local _al_number_assertions_failed Number of Assertions Line Failed
}
forvalues i = 1/`max' {
local abbreviation st
if `=substr("`i'",-1,1)' == 2 local abbreviation nd
if `=substr("`i'",-1,1)' == 3 local abbreviation rd
if inlist("`=substr("`i'",-1,1)'","4","5","6","7","8","9","0") local abbreviation th
if `i' >= 11 & `i' <= 19 local abbreviation th
local _al_assertion_details`i' Assertion Tag or Syntax for `i'`abbreviation' Failed Assertion
}
* Put the new variable name into excel file
putexcel set "`excel'.xlsx", modify sheet("`sheet'")
local n 1
foreach v of varlist* {
mata: st_local("xlcolname", invtokens(numtobase26(`n')))
if "`format'" != "" putexcel `xlcolname'1 = "``v''", txtwrap bold left fpattern("solid", "lightgray")
else putexcel `xlcolname'1 = "``v''"
local ++n
}
}
end