-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollapseandpreserve.ado
97 lines (86 loc) · 3.33 KB
/
collapseandpreserve.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
program define collapseandpreserve
// Written by Shafique Jamal ([email protected]).
// This will collapse the dataset and preserve the variable and value labels. The syntax for using this is just like with the collapse command.
// There is one additional optional option: show stat. If you add this option to the command (collapseandperserve ... ,by(...) omitstatfromvarlabel
// then it will not show the statistic (i.e. (fist), (mean), (last), etc.) in the variable label
syntax anything(id="variable and values" name=arguments equalok), by(string asis) [cw fast Omitstatfromvarlabel]
version 9.1
// save all the value labels
tempfile tf
label save using `"`tf'"', replace
// get the list of variables to be collapse, and keep track of the value label - variable correspondence
tempname precollapse_listofvars
tempname postcollapse_listofvars
tempname listofvaluelabels
tempname valuelabelname
tempname stat
tempname oldvarname
tempname newvarname
local `stat' "(mean)"
foreach a of local arguments {
// di `"word: `a'"'
if (regexm(`"`a'"',"^\(.*\)$")) { // if there is something like (first), (mean), etc.
local `stat' = `"`a'"'
}
else { // This is a variable. Store the associated variable label and value label name
// What if there is an = in the term? then need two list of variables: a precollapse list and a postcollapse list
if (regexm(`"`a'"',"^(.*)=(.*)$")) {
// di "Regex match!"
local `oldvarname' = regexs(2)
// di "oldvarname: ``oldvarname''"
local `newvarname' = regexs(1)
// di "newvarname: ``newvarname''"
}
else {
// di "NO regex match!"
local `oldvarname' `"`a'"'
// di "oldvarname: ``oldvarname''"
local `newvarname' `"`a'"'
// di "newvarname: ``newvarname''"
}
local `precollapse_listofvars' `"``precollapse_listofvars'' ``oldvarname''"'
local `postcollapse_listofvars' `"``postcollapse_listofvars'' ``newvarname''"'
local `valuelabelname' : value label ``oldvarname''
tempname vl_``newvarname''
local `vl_``newvarname''' : variable label ``oldvarname''
if (`"``vl_``newvarname''''"' == `""') {
local `vl_``newvarname''' `"``newvarname''"'
}
// di `"omitstatfromvarlabel = `omitstatfromvarlabel'"'
if (`"`omitstatfromvarlabel'"'==`""') {
local `vl_``newvarname''' `"``stat'' ``vl_``newvarname''''"'
// di "not omitting"
}
else {
local `vl_``newvarname''' `"``vl_``newvarname''''"'
// di "omitting"
}
if (`"``valuelabelname''"' == `""') { // variable has no value label
local `listofvaluelabels' `"``listofvaluelabels'' ."'
}
else {
local `listofvaluelabels' `"``listofvaluelabels'' ``valuelabelname''"'
}
}
}
collapse `arguments', by(`by') `cw' `fast'
// macro list
// retrieve the valuelabels
qui do `"`tf'"'
// reapply the variable labels and the value labels
tempname count
local `count' = 0
// di "------------------------------------------------"
foreach var of local `postcollapse_listofvars' {
// di `"var: `var'"'
// di `"its variable label: ``vl_`var'''"'
// reapply the variable labels
local `count' = ``count'' + 1
label var `var' `"``vl_`var'''"'
// reapply the value labels
local `valuelabelname' : word ``count'' of ``listofvaluelabels''
if (`"``valuelabelname''"' != `"."') {
label values `var' ``valuelabelname''
}
}
end program