-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctometa.ado
366 lines (296 loc) · 10.4 KB
/
ctometa.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
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
*! version 1.0.4 7aug2020 Aaron Wolf, [email protected]
cap program drop ctometa
program define ctometa, rclass
version 15
syntax using [, Keep(namelist) noPreserve noVALlabels noVARlabels]
if "`preserve'" != "nopreserve" preserve
qui {
// List of variables in the current dataset
qui ds
local indf `r(varlist)'
// Survey
* Import survey and confirm that keep namelist exists
import excel `using', sheet("survey") firstrow clear
foreach name in type name `keep' {
cap confirm variable `name'
if _rc != 0 {
di as err `"The field "`name'" does not exist in using XLSForm."'
exit 198
}
}
*===============================================================================*
* *
* SECTION 1: Main survey questions *
* *
*===============================================================================*
* Keep type, name and specified variables. Keep all by default.
if "`keep'" != "" keep type name `keep'
else {
ds label*
local labels `r(varlist)'
gettoken label: labels
ds hint*
local hints `r(varlist)'
gettoken hint: hints
keep type name `label' `hint'
}
// Isolate label variable
* Ensure only one label was selected
ds label*
local language = subinstr("`r(varlist)'","label","",.)
if wordcount("`r(varlist)'") > 1 {
di as error "You must keep only one label language"
exit 198
}
rename label label
cap ds hint*
if _rc ==0 & wordcount("`r(varlist)'") > 1 {
di as error "You must keep only one hint language"
exit 198
}
cap rename hint hint
* Preserve sort order
gen sort = _n
* Trim all variables to ensure extra spaces don't cause errors
qui ds, has(type string)
foreach var of varlist `r(varlist)' {
replace `var' = strtrim(stritrim(`var'))
}
* Remove begin/end group/repeat, and notes
drop if inlist(type,"begin group","begin repeat","end group","end repeat","note")
* Keep only variables in the current dataset
levelsof name, local(current)
local insheet: list current & indf
gen keepname = 0
foreach name of local insheet {
replace keepname = 1 if name == "`name'"
}
keep if keepname == 1
drop keepname
qui count
if `r(N)' > 0 {
* Get list of variables to write to metadata
ds type name sort label, not
local vlist `r(varlist)'
* Split type to get type and list
split type, parse(" ") gen(type)
replace type = type1 if inlist(type1,"select_one","select_multiple")
gen list = type2 if inlist(type1,"select_one","select_multiple"), after(type)
replace type = subinstr(type," ","_",.)
drop type?
* Create new extended type variable for each type
levelsof type, local(types)
foreach type of local types {
gen type_`type' = "1" if type == "`type'"
}
* Ensure list has proper Stata names
replace list = subinstr(list,"-","_",.)
* Create new extended list variable for each list
levelsof list, local(lists)
foreach list of local lists {
cap confirm name `list'
if _rc {
di as error "`list' is not a valid Stata name."
confirm name `list'
}
else gen list_`list' = "1" if list == "`list'"
}
// Adjust metadata for selected() relevance criteria
* Count number of "selected(" expressions
* Count number of "selected(" expressions
replace relevance = subinstr(stritrim(relevance),", ",",",.)
replace relevance = subinstr(relevance," )",")",.)
replace relevance = subinstr(relevance,"'","",.)
gen count = (strlen(relevance)-strlen(subinstr(relevance,"selected","",.)))/strlen("selected")
sum count
* Loop over all possible selected expressions and replace with expression for split variable equivalent
forvalues i = 1/`r(max)' {
gen exp`i' = regexs(0) if regexm(relevance,"selected\(\\$\{[A-Za-z0-9_]+\},([']?[-]?[0-9]+[']?)\)")
gen part1_`i' = regexs(1) if regexm(relevance,"(selected\(\\$\{)([A-Za-z0-9_]+)(\},)([']?[-]?[0-9]+[']?)(\))")
gen part2_`i' = regexs(2) if regexm(relevance,"(selected\(\\$\{)([A-Za-z0-9_]+)(\},)([']?[-]?[0-9]+[']?)(\))")
gen part3_`i' = regexs(3) if regexm(relevance,"(selected\(\\$\{)([A-Za-z0-9_]+)(\},)([']?[-]?[0-9]+[']?)(\))")
gen part4_`i' = regexs(4) if regexm(relevance,"(selected\(\\$\{)([A-Za-z0-9_]+)(\},)([']?[-]?[0-9]+[']?)(\))")
gen part5_`i' = regexs(5) if regexm(relevance,"(selected\(\\$\{)([A-Za-z0-9_]+)(\},)([']?[-]?[0-9]+[']?)(\))")
gen new`i' = part2_`i' + "_" + subinstr(part4_`i',"-","_",.) + "=1"
replace relevance = subinstr(relevance,exp`i',new`i',.)
drop exp`i' part?_`i' new`i'
}
// Adjust relevance criteria for remaining expressions
* Remaining relevance cleaning
replace relevance = subinstr(relevance,"`=char(10)'"," ",.)
replace relevance = subinstr(relevance,"}","]",.)
replace relevance = subinstr(relevance,"\${","[",.)
replace relevance = subinstr(relevance,"[","",.)
replace relevance = subinstr(relevance,"]","",.)
replace relevance = subinstr(relevance,"or","|",.)
replace relevance = subinstr(relevance,"and","&",.)
replace relevance = subinstr(relevance,"=","==",.)
replace relevance = subinstr(relevance,"<==","<=",.)
replace relevance = subinstr(relevance,">==",">=",.)
replace relevance = subinstr(relevance,"!==","!=",.)
replace relevance = subinstr(relevance,"not","!",.)
* Save survey metadata
tempfile svymeta
save `svymeta'
* Write a temporary .do file with a line creating a characteristic for each variable
cap file close metadata
tempfile metadata
* Check that the file does not exist. Delete if it does
cap confirm file `metadata'
if _rc == 0 erase `metadata'
file open metadata using `metadata', write
sort sort
drop sort
qui count
local n = `r(N)'
foreach x of varlist type list label `vlist' type_* list_* name {
cap confirm string variable `x'
if _rc {
tostring `x', replace
replace `x' = "" if `x' == "."
}
replace `x' = subinstr(subinstr(subinstr(`x',"\${","[",.),"}","]",.),"`=char(10)'"," ",.)
replace `x' = "cap char " + name + "[CTO_`x'] " + `x' if !missing(`x')
forvalues i = 1/`n' {
file write metadata (`x'[`i']) _n
}
}
*===============================================================================*
* *
* SECTION 2: Select Multiple split questions *
* *
*===============================================================================*
* Isolate list of select_multiple variable to use relevant choices list
use `svymeta', clear
* Isolate select_multiple variables
keep if type == "select_multiple"
keep type list name relevance
rename list list
levelsof list, local(choicelists)
tempfile multiple
save `multiple'
// Choices - Define Variable Labels
* Load choices
import excel `using', sheet(choices) firstrow clear
rename list_name list
keep list value label`language'
rename label label
* Ensure only one label was selected
ds label*
local language = subinstr("`r(varlist)'","label","",.)
if wordcount("`r(varlist)'") > 1 {
di as error "You must keep only one label language"
exit 198
}
rename label label
* Trim all variables to ensure extra spaces don't cause errors
qui ds, has(type string)
foreach var of varlist `r(varlist)' {
replace `var' = strtrim(stritrim(`var'))
}
tempfile choices
save `choices'
* Ensure list has proper Stata names
replace list = subinstr(list,"-","_",.)
* Keep only lists that are used by the survey
gen keeplist = 0
foreach list of local lists {
replace keeplist = 1 if list == "`list'"
}
keep if keeplist == 1
drop keeplist
* Preserve sort order
gen sort = _n
sort list sort
* Create vallabel variable
cap tostring value, replace
gen vallabel = "cap label define " + list + " " + value + " " + `"""' + label + `"""' + ", modify"
replace vallabel = subinstr(subinstr(subinstr(vallabel,"\${","[",.),"}","]",.),"`=char(10)'"," ",.)
* Write to file
qui count
forvalues i = 1/`r(N)' {
file write metadata (vallabel[`i']) _n
}
// Choices - select_multiple metadata
use `choices', clear
* Keep only lists used by survey
gen keeplist = 0
foreach list of local choicelists {
replace keeplist = 1 if list == "`list'"
}
keep if keeplist == 1
drop keeplist
qui count
if `r(N)' > 0 {
* Join by list_name
joinby list using `multiple'
order type name list value label
sort name list value label
cap confirm numeric variable value
if !_rc gen variable = subinstr(name + "_" + string(value),"-","_",.)
else gen variable = subinstr(name + "_" + value,"-","_",.)
foreach label of local labels {
replace `label' = name + "=" + `label'
}
* Create label metadata
replace type = "select_multiple_choice"
* Rename name
rename name sm_name
rename variable name
order name, last
drop value
* Create new extended type variable for each type
levelsof type, local(smtypes)
foreach type of local smtypes {
gen type_`type' = "1" if type == "`type'"
}
* Create new extended list variable for each list
levelsof list, local(smlists)
foreach list of local smlists {
gen list_`list' = "1" if list == "`list'"
}
* Write Metadata
order name, last
qui count
foreach x of varlist _all {
replace `x' = subinstr(subinstr(subinstr(`x',"\${","[",.),"}","]",.),"`=char(10)'"," ",.)
replace `x' = "cap char " + name + "[CTO_`x'] " + `x' if !missing(`x')
forvalues i = 1/`r(N)' {
file write metadata (`x'[`i']) _n
}
}
}
file close metadata
if "`preserve'" != "nopreserve" restore
else use `svymeta', clear
*/
* Run metadata .do file
qui do `metadata'
*===============================================================================*
* *
* SECTION 3: Attach CTO_list value labels to each select_one variable *
* *
*===============================================================================*
if "`vallabels'" != "novallabels" {
qui ds, has(char CTO_type_select_one)
if "`r(varlist)'" != "" {
foreach var of varlist `r(varlist)' {
cap destring `var', replace
la val `var' ``var'[CTO_list]'
}
}
}
if "`varlabels'" != "novarlabels" {
qui ds, has(char CTO_label)
if "`r(varlist)'" != "" {
foreach var of varlist `r(varlist)' {
la var `var' `"``var'[CTO_label]'"'
}
}
}
}
}
* Return local macros with types and lists used
return local types `"`types'"'
return local lists `"`lists'"'
end