-
Notifications
You must be signed in to change notification settings - Fork 6
/
dt_st_chars.mata
238 lines (197 loc) · 5.43 KB
/
dt_st_chars.mata
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
mata
/**
* @brief Get dataset characteristics from Stata as associative array
* @param evarid (optional) scalar variable number, variable name, or "_dta"
* @returns associative array
* @demo
* stata("sysuse auto, clear")
* dtachars = dt_st_chars("_dta")
* asarray_keys(dtachars)
*/
transmorphic scalar dt_getchars(| transmorphic scalar evarid)
{
transmorphic scalar A, B
string vector evarlist, charnames
string scalar evarname, charnamei, idtype
real scalar i, j, nvar, evarnum, useall
nvar = st_nvar()
idtype = eltype(evarid)
// Mata doesn't short-circuit logical tests
useall = 1
if (args() == 1) {
useall = 0
if (evarid == "") {
useall == 1
}
else if (idtype == "real") {
useall = missing(evarid)
}
}
// if !useall check for errors and get chars for specified evarid
if (!useall) {
if (idtype == "real") {
evarnum = round(evarid)
if (evarid < 1 | evarid > nvar) {
_error(3300, "argument out of range")
}
evarname = st_varname(evarnum)
}
else if (idtype == "string") {
if (evarid == substr("_dta", 1, strlen(evarid))) {
evarname = "_dta"
}
else {
evarnum = _st_varindex(evarid, 1)
if (evarnum == .) {
_error(3500, "invalid Stata variable name")
}
evarname = st_varname(evarnum)
}
}
else {
_error(3255, "string or real required")
}
// It seems that charnames cannot contain binary.
// If they can, using a local macro as below is not safe.
stata("local _devtools_local : char " + evarname + "[]")
charnames = tokens(st_local("_devtools_local"))
// make associative array
A = asarray_create()
for (i = 1; i <= length(charnames); i++) {
charnamei = charnames[i]
asarray(A, charnamei, st_global(evarname + "[" + charnamei + "]"))
}
return(A)
}
evarlist = J(1, nvar + 1, "")
evarlist[1] = "_dta"
for (i = 1; i <= nvar; i++) {
evarlist[i + 1] = st_varname(i)
}
// make associative array
A = asarray_create()
for (i = 1; i <= length(evarlist); i++) {
evarname = evarlist[i]
// It seems that charnames cannot contain binary.
// If they can, using a local macro as below is not safe.
stata("local _devtools_local : char " + evarname + "[]")
charnames = tokens(st_local("_devtools_local"))
if (length(charnames) == 0) continue
// make sub-array
B = asarray_create()
for (j = 1; j <= length(charnames); j++) {
charnamei = charnames[j]
asarray(B, charnamei, st_global(evarname + "[" + charnamei + "]"))
}
asarray(A, evarname, B)
}
return(A)
}
/**
* @brief Set Stata dataset characteristics using associative array
* @param evarid scalar variable number, variable name, or "_dta". If setting all chars use "" or a missing value.
* @param chararry associative array from which to set characteristics
*/
void dt_setchars(transmorphic scalar evarid, transmorphic scalar chararray)
{
transmorphic B
string vector evarlist, charnames
string scalar evarname, charnamei, idtype
real scalar i, j, nvar, evarnum, useall
nvar = st_nvar()
idtype = eltype(evarid)
// Mata doesn't short-circuit logical tests
useall = 0
if (evarid == "") {
useall == 1
}
else if (idtype == "real") {
useall = missing(evarid)
}
// if !useall check for errors and get chars for specified evarid
if (!useall) {
if (idtype == "real") {
evarnum = round(evarid)
if (evarid < 1 | evarid > nvar) {
_error(3300, "argument out of range")
}
evarname = st_varname(evarnum)
}
else if (idtype == "string") {
if (evarid == substr("_dta", 1, strlen(evarid))) {
evarname = "_dta"
}
else {
evarnum = _st_varindex(evarid, 1)
if (evarnum == .) {
_error(3500, "invalid Stata variable name")
}
evarname = st_varname(evarnum)
}
}
else {
_error(3255, "string or real required")
}
charnames = asarray_keys(chararray)
for (i = 1; i <= length(charnames); i++) {
charnamei = charnames[i]
st_global(
evarname + "[" + charnamei + "]",
asarray(chararray, charnamei)
)
}
}
evarlist = J(1, nvar + 1, "")
evarlist[1] = "_dta"
for (i = 1; i <= nvar; i++) {
evarlist[i + 1] = st_varname(i)
}
for (i = 1; i <= length(evarlist); i++) {
evarname = evarlist[i]
B = asarray(chararray, evarname)
if (B == J(0,0,.)) continue
charnames = asarray_keys(B)
for (j = 1; j <= length(charnames); j++) {
charnamei = charnames[j]
st_global(
evarname + "[" + charnamei + "]",
asarray(B, charnamei)
)
}
}
}
/**
* @brief Get value label from Stata as associative array, or set value label from associative array
* @param vlname scalar containing value label name
* @param vlarray (optional) associative array with integer keys and string contents
* @returns J(0,0,.) (void) if setting value vabel, associative array if retreiving value label
*/
transmorphic dt_vlasarray(string scalar vlname, | transmorphic scalar vlarray)
{
transmorphic scalar A
string vector text
real vector values
real scalar i
// args() == 1 get associative array
if (args() == 1) {
A = asarray_create("real")
if (st_vlexists(vlname)) {
st_vlload(vlname, values, text)
for (i = 1; i <= length(values); i++) {
asarray(A, values[i], text[i])
}
}
return(A)
}
// if args() == 2 set value label
if (st_vlexists(vlname)) {
st_vldrop(vlname)
}
values = asarray_keys(vlarray)
text = J(rows(values), cols(values), "")
for (i = 1; i <= length(values); i++) {
text[i] = asarray(vlarray, floor(values[i]))
}
st_vlmodify(vlname, values, text)
}
end