------------------------------------------------------------------------------- help formatsaveandmatload(contactMarc Muendler) -------------------------------------------------------------------------------

Save and load matrices

matsavematrix[,replacesavingdropallpath(path)type(type)]

matloadfilename[,savingdropallpath(path)missing(value)rowname(variable)overwrite]

Description

matsavesaves a matrix from memory to disk as a Stata dataset (matrix.dta).matloadloads a matrix that has been saved withmatsave. matsave and matload are based on Stata commands svmat and mkmat (mkmat).

matsavecreates a Stata dataset. The variable names correspond to the column names ofmatrix. Column names must be unique. matsave also creates a variable_rownamecontaining the row names ofmatrix.matrixis saved aspath\matrix.dta. (To assign column and row names, see matrname.)

matloadexpects a variable_rownamefor backward conversion, or a variable that takes the role of_rownamemust be specified.

Common Options

savingforces matsave and matload to save the data that are presently in memory and to restore the data upon completion. Settingsavingdoes not affect a precedingpreservecommand. If there are data in memory andsavingis not specified, matsave and matload will abort.

dropallmakes matsave and matload drop data from memory if present. The data will not be restored upon completion. dropall may not be abbreviated.

path()supplies the path where matsave savesmatrixand from where matload loadsmatrix.dta. Thepathnameinpath(pathname)needs to be a string. Ifpath()is not specified, then matsave and matload expect files in the current Stata working directory.

Options for matsave

replacepermits matsave to overwrite an existing datasetmatrix.dta. replace may not be abbreviated.

type(type)makes matsave save all variables astype. The defaulttypeis float.

Options for matload

missing(value). Matrices cannot contain missing entries in versions of Stata before 8. The optionmissing(value)replaces any missing values in the filematrix.dtawith the specifiedvaluewhen loading the file. The defaultvalueis -999.

rowname(variable). matload expects a variable_rownamefor backward conversion. Alternatively, the optionrowname(variable)renames the variablevariablein the filematrix.dtato_rownamewhen loading the file.

overwritepermits matload to overwrite an existing matrix in memory with the datasetmatrix.dtafrom disk.

Remarks on matsaveA typical use of matsave is

. matsave A, p("matrices") dropall

This saves matrix

Ato subdirectory "dir\matrices", wheredirstands for the current Stata working directory. The optiondropallworks like a preceding drop _all command and removes all data from memory.matsave uses the Stata command

svmat, and svmat requires that data are removed from memory. If a dataset is in memory, an error message would result. matsave A no; data in memory would be lost r(4);

matsave can remove and restore the data for you. Under the option

saving, matsave saves the current dataset temporarily, drops all variables, and restores the data upon completion.. matsave A, replace p("matrices") saving

The option saving is different from

preservein that it can be applied in addition to a preceding preserve command. In other words,savingallows a double preservation of data.The option saving can be time consuming, however, if your dataset in memory is large. Dropping all variables can be more efficient when many matrices are saved or loaded.

. preserve . matsave A, replace p("matrices") dropall . matsave B, replace p("matrices") dropall

rest of code. restoreIf the path "

dir\matrices\" does not exist, matsave will result in the error. matsave A, replace p("matrices") dropall error (after svmat was applied) file could not be opened r(603);

Remarks on matloadThe command

. matload A, p("matrices") saving over

loads the saved matrix

Aback into memory. matload is based on the Stata commandmkmatand also requires temporary removal of data from memory.Under the option

overwrite, matload replaces the current matrixAin memory with the one loaded from "dir\matrices\A.dta". Without the option overwrite, matload would have responded with an error message.. matload A, p("matrices") no; matrix A would be lost r(4);

A note on column namesmatsave saves the row names of

matrixin a variable_rowname. The column names ofmatrixwill become the variable names inmatrix.dta. Column names must be unique. If a column ofmatrixis called "_cons", "_b" or "_coef", the according variables will be named "__cons", "__b" or "__coef" inmatrix.dta("_cons", "_b" and "_coef" are protected system variables). matload expects the variable_rownamefor backward conversion. matload transforms column names "__cons", "__b" or "__coef" back to "_cons", "_b" or "_coef".To assign column and row names to

matrix, see matrname.The name of

matrixis not allowed to coincide with a variable name in memory.

Examples. matsave A, p("matrices") s replace . matload A, p("matrices") s over

AuthorMarc-Andreas Muendler, Assistant Professor, Department of Economics, University of California, San Diego. URL: http://econ.ucsd.edu/~muendler/, Email: muendler@econ.ucsd.edu.

Also seeManual:

[P] matrix utilityOn-line: help for matrix, mkmat, matrname