At 17:21 15/08/2004, Oleksandr Shepotylo. wrote:
>Hi ,
>
>Is there a way to save and load matrices in text format?
>Suppose I run Stata and want to use matrix A in another program X.
>So, I want to save A in csv or txt form. Then I do some manipulations
>with A and want to load A back in Stata while keeping all my data
unchanged.
and Roger replied...
To save a matrix to a text file, se -svmat- to copy a matrix to a
dataset
in memory, and -outsheet- to write that dataset to a disk file. To load
a
matrix from a text file, use -insheet- to read from the text file into a
dataset in memory, and -mkmat- to copy the dataset into a matrix. To
find
more about -insheet- and -outsheet-, see -[R] insheet- and -[R]
outsheet-.
To find more about -mkmat- and -svmat-, see the Programming manual under
-[P] matrix mkmat
But this does not really address Oleksandr's desire to load a matrix
WHIILE KEEPING ALL MY DATA UNCHANGED.
Thankfully Bill Gould produced some sample code to do just this some
time ago, when the -file- command was added to Stata 7. Way back when I
lifted that code from -help file- and put it up on my Unix server's
Stata, but never shared it via SSC. I have put the code up in SSC
package matin4-matout4. They have some rough edges, but the example
below shows precisely how
they may be used in the middle of a Stata session without disturbing
the contents of the current dataset. Note that the matrix name is not
preserved, so that one can read it in under another name (which might
be very useful, if you're trying to compare two matrices which
originally had the same name).
. ssc install matin4-matout4
(output suppressed)
. webuse auto
(1978 Automobile Data)
. reg price mpg rep78 headroom
Source | SS df MS Number of obs =
69
-------------+------------------------------ F( 3, 65) =
7.51
Model | 148497605 3 49499201.8 Prob > F =
0.0002
Residual | 428299354 65 6589220.82 R-squared =
0.2575
-------------+------------------------------ Adj R-squared =
0.2232
Total | 576796959 68 8482308.22 Root MSE =
2566.9