Library to read in v5/v6 XPORT files using Node.js .
To add xport-js to your project, run
npm install xport-js
See the library documentation for details on methods and options available.
import Library from 'xport-js';
// Create a library instance
const lib = new Library('/path/to/ds.xpt');
// Get variable metadata
const metadata = await lib.getMetadata();
// Get dataset records as objects
let records = [];
for await (let obs of lib.read({ rowFormat: 'object' })) {
records.push(obs);
}
// Output contents of XPT file to CSV file(s)
await lib.toCsv('/outDir/')
This method return AsyncIterable which can be used in for await ... of statement.
lib.read(options);
- dsNames List of dataset names to read, by default all datasets are read.
- rowFormat [default=array] Output observation format. array: [value1, value2, value3, ...] object: { var1: value1, var: value2, var3: value3, ... }
- keep [default=[]] Array of variables to keep in the result (case-insensitive)
- skipHeader [default=false] Flag to control whether the first record contains variable names.
- encoding [default=binary] String encoding, default is latin1 (binary). See the list of encodings supported by Node.js.
Creates CSV file(s) in the outDir.
lib.read(outDir, options);
See read method options description for details.
Returns unique values for specified columns in the dataset.
const lib = new Library('path/to/file.xpt');
const unique = await lib.getUniqueValues({
columns: ['AGE', 'SEX', 'RACE'],
limit: 10, // Optional: max number of unique values per column (0 = no limit)
addCount: true, // Optional: include counts for each unique value
sort: true, // Optional: sort unique values
roundPrecision: 0 // Optional: round numeric values
});
columns
(string[]): List of variable names to get unique values for.limit
(number, optional): Maximum number of unique values per column (default: 0, no limit).addCount
(boolean, optional): Whether to include counts for each unique value (default: false).sort
(boolean, optional): Whether to sort the unique values (default: false).roundPrecision
(number, optional): Rounds numeric values to the specified precision.
A Promise resolving to an object where each key is a column name and the value is an object with:
values
: Array of unique values for that column.counts
: (ifaddCount
is true) Object mapping value to count.
This project is licensed under the MIT License - see the LICENSE file for details.