-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatwrite.ado
167 lines (114 loc) · 4.07 KB
/
matwrite.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
*! matwrite version 0.94. Copyright (c) 2004-2010 Andrew Shephard
cap program define _matwrite, plugin using("matwrite.plugin")
program define matwrite
version 10.1
syntax [anything] using/ [if] [in] [,MATrix(namelist)] [,replace]
local matrix : list retokenize matrix
local matrix : list uniq matrix
local anything : list retokenize anything
if ("`matrix'"=="") local matrix : all matrices
qui de
if (`r(k)'>0) {
if ("`anything'"=="") unab anything: *
}
if (((`r(k)'==0)|(`r(N)'==0)) & ("`matrix'"=="")) {
di as error "no variables or matrices defined"
exit 111
}
/* add file extension if needed */
if substr("`using'",length("`using'")-3,4)~=".mat" {
local using = "`using'" + ".mat"
}
local anything : subinstr local anything "[" " [ ", all
local anything : subinstr local anything "]" " ] ", all
local nVarList : list sizeof anything
local ++nVarList
local isMatrix = 0
/* create lists to be passed to plugin */
forval n = 1/`nVarList' {
tokenize `anything'
if ("`1'"=="[") {
if (`isMatrix'==1) error 198
if (index("`matNameList' `newVarList' ","`lastVar' ")>0) {
di as error "a matrix has already been defined with name `lastVar'"
exit 110
}
local startSize : list sizeof matVarList
local matNameListOld `matNameList'
local matNameList `"`matNameList' `lastVar'"'
local isMatrix = 1
local thisMatrix `lastVar'
local lastVar ""
}
else if ("`1'"=="]") {
if "`lastVar'"~="" {
unab lastVar : `lastVar'
if (index(" `matNameList' "," `lastVar' ")>0) & (`isMatrix'==0) {
di as error "a matrix has already been defined with name `lastVar'"
exit 110
}
foreach var of varlist `lastVar' {
local tempType : type `var'
if (substr("`tempType'",1,3)=="str") {
di as text "warning: ignoring string variable `var'"
local lastVar : subinstr local lastVar "`var'" ""
}
}
local matVarList `"`matVarList' `lastVar'"'
local lastVar ""
}
local endSize : list sizeof matVarList
local size = `endSize' - `startSize'
local matNameList `"`matNameList' `size'"'
if (`startSize'==`endSize') {
local matNameList `matNameListOld'
local matIndexList `matIndexListOld'
di as text "warning: ignoring matrix `thisMatrix' with zero dimension"
}
local isMatrix = 0
}
else {
if ("`lastVar'"~="") {
unab lastVar : `lastVar'
if (index(" `matNameList' "," `lastVar' ")>0) & (`isMatrix'==0) {
di as error "a matrix has already been defined with name `lastVar'"
exit 110
}
foreach var of varlist `lastVar' {
local tempType : type `var'
if (substr("`tempType'",1,3)=="str") {
di as text "warning: ignoring string variable `var'"
local lastVar : subinstr local lastVar "`var'" ""
}
}
if (`isMatrix'==0) local newVarList `"`newVarList' `lastVar'"'
if (`isMatrix'==1) local matVarList `"`matVarList' `lastVar'"'
}
local lastVar `1'
}
mac shift
local anything `*'
}
local newVarList : list uniq newVarList
local matSize : list sizeof matNameList
local matSize = 0.5*`matSize'
foreach stataMat in `matrix' {
if (index(" `matNameList' `newVarList' "," `stataMat' ")>0) {
di as text "warning: ignoring Stata matrix `stataMat'. A matrix has been defined with this name"
local matrix : subinstr local matrix "`stataMat'" ""
}
}
local version = 1 //0 for file append
/* variable names to be read from local macro */
local allNames `"`matNameList' `newVarList' `matrix'"'
foreach varName in `allNames' {
if (length("`varName'")>19) {
di as error "the variable name `varName' is too long"
di as error "variable and matrix names can not exceed 19 characters"
exit 198
}
}
local allNamesLen : length local allNames
plugin call _matwrite `matVarList' `newVarList' `if' `in', "`using'" `version' `replace' `matSize' `allNamesLen'
di as text "file `using' saved"
end