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);
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: (if addCount is true) Object mapping value to count.This project is licensed under the MIT License - see the LICENSE file for details.