-
Notifications
You must be signed in to change notification settings - Fork 29
/
matexport.ado
191 lines (158 loc) · 4.7 KB
/
matexport.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
// documentation written for markdoc
/***
Version: 1.0.0
matexport
=========
export matrices from Stata as a data set
Syntax
------
matexport `mat', rnames(name) filename(filename) version(13)
the command exports a data set from a given matrix with the given name and
includes the matrix row names in a column named
Citation
--------
The program is based on a command written by __Nicholas J. Cox__, in a package
called __dm79__, from which I read and borrowed a command named
__svmat2__. I have used the version 1.2.2
Since the package is hosted on SSC and cannot be specified as a dependency,
I had to embed it in this package with a different name.
Author
------
__E. F. Haghish__
Department of Psychology
University of Oslo
haghish@hotmail.com
[rcall Homepage](www.haghish.com/packages/Rcall.php)
Package Updates on [Twitter](http://www.twitter.com/Haghish)
- - -
This help file was dynamically produced by {help markdoc:MarkDoc Literate Programming package}
***/
*cap prog drop matexport
program define matexport
version 5.0
preserve
quietly drop _all
quietly label drop _all
// Syntax processing
// ------------------------------------------------------------
parse "`*'", parse(" ,")
// specify the matrix name as `A' and make sure it exists, otherwise, return an error
if "`2'" == "" | "`2'" == "," {
local type "float"
local A "`1'"
macro shift
}
else {
local type "`1'"
local A "`2'"
macro shift 2
}
capture local nc = colsof(matrix(`A'))
if _rc {
di in red "matrix `A' not found"
exit 111
}
local options "Rnames(string) Full filename(string) version(integer 13)"
parse "`*'"
if "`rnames'" != "" { confirm new variable `rnames' }
local j 1
// allways get the matrix column names from the columns
local names = "col"
local notc "*"
`nots' local cnames : colnames(`A')
`notc' `notm' `nots' local enames : coleq(`A')
while `j' <= `nc' {
`nots' local varnam : word `j' of `cnames'
`notc' `notm' `nots' local pre : word `j' of `enames'
`notc' `noteq' `nots' local pre "`A'"
`notc' `nots' local varnam = substr("`pre'`varnam'",1,8)
`nots' local vars "`vars' `varnam'"
`notc' `noteq' `notm' if `stub' { local vars "`vars' `names'`j'" }
`notc' `noteq' `notm' if !`stub' { local vars "`names'" }
local j = `j' + 1
}
local varlist "new"
capture parse "`type'(`vars')"
if _rc == 110 {
di in red "new variables cannot be uniquely named or already defined"
exit _rc
}
if _rc == 900 | _rc == 902 { noroom2 `type' `nc' _rc }
if _rc { error _rc }
drop `varlist'
parse "`varlist'", parse(" ")
local nr = rowsof(matrix(`A'))
local old_N = _N
if `nr' > _N {
di in blu "number of observations will be reset to `nr'"
di in blu "Press any key to continue, or Break to abort"
more
set obs `nr'
}
capture {
local j 1
while `j' <= `nc' {
tempvar y`j'
gen `type' `y`j'' = matrix(`A'[_n, `j']) in 1/`nr'
local j = `j' + 1
}
local j 1
while `j' <= `nc' {
rename `y`j'' ``j''
local j = `j' + 1
}
}
if _rc {
if _N > `old_N' { quietly drop if _n > `old_N' }
error _rc
}
qui if "`rnames'" != "" {
gen str1 `rnames' = ""
* trap and ignore any user -full- if < 6
capture di _caller( )
if _rc == 0 { local full "`full'" } /* it is 6 or more */
else {
if "`full'" == "full" {
noi di in bl "full option not supported"
local full ""
}
}
local Rnames : row`full'names(`A')
local i = 1
while `i' <= `nr' {
local rname : word `i' of `Rnames'
replace `rnames' = "`rname'" in `i'
local i = `i' + 1
}
}
// save the data and replace it
quietly saveold "`filename'.dta", version(`version') replace
restore
end
*cap prog drop noroom2
program define noroom2 /* `type' `nc' _rc */
version 4.0
local type "`1'"
local nc `2'
local rc `3'
if "`type'"=="float" { local w 4 }
else if "`type'"=="double" { local w 8 }
else if "`type'"=="long" { local w 4 }
else if "`type'"=="int" { local w 2 }
else if "`type'"=="byte" { local w 1 }
local w = `w'*`nc'
di as err "no room to add more variables"
di as txt "room for `nc' additional variables and additional width of `w' required"
exit `rc'
end
// -------------------------------------------------------------------------
// CREATE help file
// =========================================================================
*markdoc matexport.ado, export(sthlp) replace
/*
clear
matrix drop _all
qui sysuse auto, clear
mkmat price mpg , matrix(auto) rownames(make)
mat list auto
quietly matexport auto, rnames("MATR0WNAMES") filename("_send.matrix.sss") version(13)