Skip to content
@deckyard/statfiles

statfiles cheatsheet

Every export of @deckyard/statfiles. Guides are listed in the introduction.

jennifer
import "@deckyard/statfiles/" as statfiles;

A reader takes the file as bytes (fs.readBytes) and returns a Dataset. A malformed file or a bad argument throws Error{kind: "statfiles"}; a file over a cap (Options.maxCells, the unpacked size of compressed SPSS cases or of a compressed .sas7bdat, a .sas7bcat's size or label count) is kind "limit". A name is a variable's name exactly as stored; a row counts from 0.

Constants

ConstantValueMeaning
VERSION"0.1.0"this deck's version
ERROR_KIND"statfiles"the kind of a malformed file or a bad argument
DEFAULT_MAX_CELLS20000000the most cells (rows times variables) read
DEFAULT_MAX_BYTES268435456the most bytes compressed SPSS cases or the rows of a compressed .sas7bdat unpack to
MISSING_SYSTEM"."the code of a system-missing cell
MISSING_USER"user"the code of a value SPSS declares missing
LOWEST-1.7976931348623157e308an SPSS missing range's open low end (LO)
HIGHEST1.7976931348623157e308an SPSS missing range's open high end (HI)

Reading

FunctionReturnsDoes
read(data)Datasetdetect the format and read it with the default options
readWith(data, opts)Datasetthe same with Options
options()Optionsthe defaults: the file's own encoding, DEFAULT_MAX_CELLS, DEFAULT_MAX_BYTES
detect(data)stringdta, sav, zsav, por, xpt, sas7bdat, sas7bcat (a catalog, which read refuses), or "" for none of them
readDta(data, opts)DatasetStata .dta, formats 104 to 119, either byte order
readSav(data, opts)DatasetSPSS .sav (uncompressed or bytecode compressed, either byte order) or .zsav
readPor(data, opts)DatasetSPSS portable .por, through its translation table
readXpt(data, opts)DatasetSAS XPORT .xpt, version 5 or 8, of one member; several members are an error
readXptMembers(data, opts)list of Datasetevery member of an XPORT file; maxCells caps them together
readSas7bdat(data, opts)DatasetSAS .sas7bdat: 32- or 64-bit, either byte order, uncompressed, RLE or RDC; deleted rows left out

SAS value labels

FunctionReturnsDoes
readSas7bcat(data, opts)map of string to map of string to stringthe value labels of a SAS format catalog (.sas7bcat): format name (GENDER, $GENDER) to value to label; a format with no single-value label is left out
withSasLabels(ds, catalog)Datasetds with each variable's valueLabels from the catalog format its format names
sasFormatName(format)stringa display format's catalog name: $GENDER6. is $GENDER, 8.2 is ""

Cells and columns

FunctionReturnsDoes
names(ds)list of stringthe variable names, in file order
indexOf(ds, name)inta variable's index, -1 if there is none
variable(ds, name)Variableone variable
column(ds, name)list of floatevery value of a numeric variable; a missing cell is an error
present(ds, name)list of floatthe values that are not missing, in row order
textColumn(ds, name)list of stringevery value of a string variable
number(ds, name, row)floatone numeric cell; an SPSS user-missing value is returned, any other missing cell is an error
text(ds, name, row)stringone cell as text; a missing number as its code
missingCode(ds, name, row)string"", ., .a to .z, ._ or user
isMissing(ds, name, row)boolthe cell is missing, for any reason
labelOf(ds, name, row)stringthe cell's value label, "" if none
valueKey(x)stringa number as value labels are keyed: 3, -1, 2.5
toCsv(ds, labels)stringthe dataset as CSV, a missing number empty; labels writes value labels instead of values

Types

Dataset

FieldMeaning
format as stringdta, sav, zsav, por, xpt or sas7bdat
version as stringStata's format (118), XPORT's 5 or 8, the SAS release (9.0401M5), "" for SPSS
name as stringan XPORT member's or SAS data set's name, else ""
label as stringthe dataset's label, "" if none
encoding as stringthe character set the text was decoded from
rowCount as intthe number of rows
variables as list of Variablethe variables, in file order
numbers as list of list of floata numeric variable's values by row (0.0 where missing); [] for a string variable
texts as list of list of stringa string variable's values by row (Stata's with their trailing blanks, SPSS's and SAS's without); [] for a numeric variable
missing as list of list of stringeach cell's missing code, "" when present

Variable

FieldMeaning
name as stringthe name, as stored
label as stringits label, "" if none
type as stringnumeric or string
storage as stringthe file's type: Stata byte ... double, str<n>, strL; SPSS and SAS numeric / string
width as intthe bytes a value takes: a string variable's width (0 for a Stata strL); a number's stored size (Stata 1 to 8, .sav / .zsav 8, SAS 3 to 8, XPORT 2 to 8, .por 0)
format as stringthe display format: %9.0g, F8.2, DATE9.
valueLabels as map of string to stringvalue labels, keyed by valueKey (Stata .a keyed ".a")
missingValues as list of stringvalues SPSS declares missing, as valueKey writes them
missingRange as list of floatan SPSS missing range [low, high], or []

Options

FieldMeaning
encoding as stringthe text's character set, overriding the file's; "" for the file's own
maxCells as intthe most cells read, or value labels read from a .sas7bcat; kind "limit" beyond
maxBytes as intthe most bytes compressed SPSS cases (.zsav zlib blocks, and bytecode in the uncompressed layout) or the rows of a compressed .sas7bdat unpack to, and the largest .sas7bcat; kind "limit" beyond