readstat

Command-line tool (+ C library) for converting SAS, Stata, and SPSS files 💾

https://github.com/wizardmac/readstat

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • â—‹
    CITATION.cff file
  • ✓
    codemeta.json file
    Found codemeta.json file
  • ✓
    .zenodo.json file
    Found .zenodo.json file
  • â—‹
    DOI references
  • â—‹
    Academic publication links
  • ✓
    Committers with academic emails
    2 of 32 committers (6.3%) from academic institutions
  • â—‹
    Institutional organization owner
  • â—‹
    JOSS paper metadata
  • â—‹
    Scientific vocabulary similarity
    Low similarity (15.5%) to scientific vocabulary

Keywords

readstat sas sas7bdat spss stata

Keywords from Contributors

fuzz-testing fuzzing pde grammar data-manipulation differentialequations ode ordinary-differential-equations sciml sde
Last synced: 6 months ago · JSON representation

Repository

Command-line tool (+ C library) for converting SAS, Stata, and SPSS files 💾

Basic Info
  • Host: GitHub
  • Owner: WizardMac
  • License: mit
  • Language: C
  • Default Branch: dev
  • Homepage:
  • Size: 2.18 MB
Statistics
  • Stars: 291
  • Watchers: 19
  • Forks: 77
  • Open Issues: 45
  • Releases: 26
Topics
readstat sas sas7bdat spss stata
Created over 13 years ago · Last pushed 9 months ago
Metadata Files
Readme Changelog License

README.md

GitHub CI build status Appveyor build status codecov Fuzzing Status

ReadStat: Read (and write) data sets from SAS, Stata, and SPSS

Originally developed for Wizard, ReadStat is a command-line tool and MIT-licensed C library for reading files from popular stats packages. Supported data formats include:

  • SAS: SAS7BDAT (binary file) and XPORT (transport file)
  • Stata: DTA (binary file) versions 104-119
  • SPSS: POR (portable file), SAV (binary file), and ZSAV (compressed binary)

Supported metadata formats include:

  • SAS: SAS7BCAT (catalog file) and .sas (command file)
  • Stata: .dct (dictionary file)
  • SPSS: .sps (command file)

There is also write support for all the data formats, but not the metadata formats. The produced SAS7BDAT files still cannot be read by SAS, but feel free to contribute your binary-format expertise here.

For reading in R data files, please see the related librdata project.

Installation on Unix / macOS

Grab the latest release and then proceed as usual:

./configure
make
sudo make install

If you're cloning the repository, first make sure you have autotools installed, and then run ./autogen.sh to generate the configure file.

If you're on Mac and see errors about AM_ICONV when you run ./autogen.sh, you'll need to install gettext.

Installation on Windows

ReadStat now includes a Microsoft Visual Studio project file that includes build targets for the library and tests. See the VS17 folder in the downloaded release for a "one-click" Windows build.

Alternatively, you can build ReadStat on the command line using an msys2 environment. After installing msys2, download some other packages:

pacman -S autoconf automake libtool make mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmake mingw-w64-x86_64-libiconv

Then start a MINGW command line (not the msys2 prompt!) and follow the UNIX install instructions above for this package.

Language Bindings

Docker

A dockerized version is available here

Command-line Usage

Standard usage:

readstat [-f] <input file> <output file>

Where:

  • <input file> ends with .dta, .por, .sav, .sas7bdat, or .xptand
  • <output file> ends with .dta, .por, .sav, .sas7bdat, .xpt or .csv

If libxlsxwriter is found at compile-time, an XLSX file (ending in .xlsx) can be written instead.

If zlib is found at compile-time, compressed SPSS files (.zsav) can be read and written as well.

Use the -f option to overwrite an existing output file.

If you have a plain-text file described by a Stata dictionary file, a SAS command file, or an SPSS command file, a second invocation style is supported:

readstat <input file> <dictionary file> <output file>

Where:

  • <input file> can be anything
  • <dictionary file> ends with .dct, .sas, or .sps
  • <output file> ends with .dta, .por, .sav, .xpt, or .csv

If you have a SAS catalog file containing the data set's value labels, you can use the same invocation:

readstat <input file> <catalog file> <output file>

Except where:

  • <input file> ends with .sas7bdat
  • <catalog file> ends with .sas7bcat
  • <output file> ends with .dta, .por, .sav, .xpt, or .csv

If the file conversion succeeds, ReadStat will report the number of rows and variables converted, e.g.

Converted 111 variables and 160851 rows in 12.36 seconds

At the moment value labels are supported, but the finer nuances of converting format strings (e.g. %8.2g) are not.

Command-line Usage with CSV input

A prerequisite for CSV input is that the libcsv library is found at compile time.

CSV input is supported together with a metadata file describing the data:

readstat <input file.csv> <input metadata.json> <output file>

The <output file> should end with .dta, .sav, or .csv.

The <input file.csv> is a regular CSV file.

The <input metadata.json> is a JSON file describing column types, value labels and missing values. The easiest way to create such a metadata file is to use the provided extract_metadata program on an existing file:

$ extract_metadata <input file.(dta|sav|sas7bcat)>

The schema of this JSON file is fully described in variablemetadata_schema.json using JSON Schema.

The following is an example of a valid metadata file:

{
    "type": "SPSS",
    "variables": [
        {
            "type": "NUMERIC",
            "name": "citizenship",
            "label": "Citizenship of respondent",
            "categories": [
                {
                    "code": 1,
                    "label": "Afghanistan"
                },
                {
                    "code": 2,
                    "label": "Albania"
                },
                {
                    "code": 98,
                    "label": "No answer"
                },
                {
                    "code": 99,
                    "label": "Not applicable"
                }
            ],
            "missing": {
                "type": "DISCRETE",
                "values": [
                    98,
                    99
                ]
            }
        }
    ]
}

Here the column citizenship is a numeric column with four possible values 1, 2, 98, and 99. 1 has the label Afghanistan, 2 has Albania, 98 has No answer and 99 has Not applicable. 98 and 99 are defined as missing values.

Other column types are STRING and DATE. All values in DATE columns are expected to conform to ISO 8601 date. Here is an example of DATE metadata:

{
    "type": "SPSS",
    "variables": [
        {
            "type": "DATE",
            "name": "startdate",
            "label": "Start date",
            "categories": [
                {
                    "code": "6666-01-01",
                    "label": "no date available"
                }
            ],
            "missing": {
                "type": "DISCRETE",
                "values": [
                    "6666-01-01",
                    "9999-01-01"
                ]
            }
        }
    ]
}

Value labels are supported for DATE.

The last column type is STRING:

{
    "type": "SPSS",
    "variables": [
        {
            "type": "STRING",
            "name": "somestring",
            "label": "Label of column",
            "missing": {
                "type": "DISCRETE",
                "values": [
                    "NA",
                    "N/A"
                ]
            }
        }
    ]
}

Value labels are not supported for STRING.

Library Usage: Reading Files

The ReadStat API is callback-based. It uses very little memory, and is suitable for programs with progress bars. ReadStat uses iconv to automatically transcode text data into UTF-8, so you don't have to worry about character encodings.

See src/readstat.h for the complete API. In general you'll provide a filename and a set of optional callback functions for handling various information and data found in the file. It's up to the user to store this information in an appropriate data structure. If a context pointer is passed to the parse_* functions, it will be made available to the various callback functions.

Callback functions should return READSTAT_HANDLER_OK (zero) on success. Returning READSTAT_HANDLER_ABORT will abort the parsing process.

Example: Return the number of records in a DTA file.

```c

include "readstat.h"

int handlemetadata(readstatmetadatat *metadata, void *ctx) { int *mycount = (int *)ctx;

*my_count = readstat_get_row_count(metadata);

return READSTAT_HANDLER_OK;

}

int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s \n", argv[0]); return 1; } int mycount = 0; readstaterrort error = READSTATOK; readstatparsert *parser = readstatparserinit(); readstatsetmetadatahandler(parser, &handlemetadata);

error = readstat_parse_dta(parser, argv[1], &my_count);

readstat_parser_free(parser);

if (error != READSTAT_OK) {
    printf("Error processing %s: %d\n", argv[1], error);
    return 1;
}
printf("Found %d records\n", my_count);
return 0;

} ```

Example: Convert a DTA to a tab-separated file.

```c

include "readstat.h"

int handlemetadata(readstatmetadatat *metadata, void *ctx) { int *myvar_count = (int *)ctx;

*my_var_count = readstat_get_var_count(metadata);

return READSTAT_HANDLER_OK;

}

int handlevariable(int index, readstatvariablet *variable, const char *vallabels, void *ctx) { int *myvarcount = (int *)ctx;

printf("%s", readstat_variable_get_name(variable));
if (index == *my_var_count - 1) {
    printf("\n");
} else {
    printf("\t");
}

return READSTAT_HANDLER_OK;

}

int handlevalue(int obsindex, readstatvariablet *variable, readstatvaluet value, void *ctx) { int *myvarcount = (int *)ctx; int varindex = readstatvariablegetindex(variable); readstattypet type = readstatvaluetype(value); if (!readstatvalueissystemmissing(value)) { if (type == READSTATTYPESTRING) { printf("%s", readstatstringvalue(value)); } else if (type == READSTATTYPEINT8) { printf("%hhd", readstatint8value(value)); } else if (type == READSTATTYPEINT16) { printf("%hd", readstatint16value(value)); } else if (type == READSTATTYPEINT32) { printf("%d", readstatint32value(value)); } else if (type == READSTATTYPEFLOAT) { printf("%f", readstatfloatvalue(value)); } else if (type == READSTATTYPEDOUBLE) { printf("%lf", readstatdoublevalue(value)); } } if (varindex == *myvar_count - 1) { printf("\n"); } else { printf("\t"); }

return READSTAT_HANDLER_OK;

}

int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s \n", argv[0]); return 1; } int myvarcount = 0; readstaterrort error = READSTATOK; readstatparsert *parser = readstatparserinit(); readstatsetmetadatahandler(parser, &handlemetadata); readstatsetvariablehandler(parser, &handlevariable); readstatsetvaluehandler(parser, &handle_value);

error = readstat_parse_dta(parser, argv[1], &my_var_count);

readstat_parser_free(parser);

if (error != READSTAT_OK) {
    printf("Error processing %s: %d\n", argv[1], error);
    return 1;
}
return 0;

} ```

Library Usage: Writing Files

ReadStat can write data sets to a number of file formats, and uses largely the same API for each of them. Files are written incrementally, with the header written first, followed by individual rows of data, and ending with some kind of trailer. (So the full data file never resides in memory.) Unlike like the callback-based API for reading files, the writer API consists of function that the developer must call in a particular order. The complete API can be found in readstat.h.

Basic usage:

```c

include "readstat.h"

/* A callback for writing bytes to your file descriptor of choice / / The ctx argument comes from the readstatbeginwritingxxx function */ static ssizet writebytes(const void *data, sizet len, void *ctx) { int fd = *(int *)ctx; return write(fd, data, len); }

int main(int argc, char *argv[]) { readstatwritert *writer = readstatwriterinit(); readstatsetdatawriter(writer, &writebytes); readstatwritersetfilelabel(writer, "My data set");

int row_count = 1;

readstat_variable_t *variable = readstat_add_variable(writer, "Var1", READSTAT_TYPE_DOUBLE, 0);
readstat_variable_set_label(variable, "First variable");

/* Call one of:
 *   readstat_begin_writing_dta
 *   readstat_begin_writing_por
 *   readstat_begin_writing_sas7bdat
 *   readstat_begin_writing_sav
 *   readstat_begin_writing_xport
 */

int fd = open("something.dta", O_CREAT | O_WRONLY);
readstat_begin_writing_dta(writer, &fd, row_count);

int i;
for (i=0; i<row_count; i++) {
    readstat_begin_row(writer);
    readstat_insert_double_value(writer, variable, 1.0 * i);
    readstat_end_row(writer);
}

readstat_end_writing(writer);
readstat_writer_free(writer);
close(fd);

return 0;

} ```

Fuzz Testing

To assist in fuzz testing, ReadStat ships with target files designed to work with libFuzzer. Clang 6 or later is required.

  1. ./configure --enable-fuzz-testing turns on useful sanitizer and sanitizer-coverage flags
  2. make will create a new binary called generate_corpus. Running this program will use the ReadStat test suite to create a corpus of test files in corpus/. There is a subdirectory for each sub-format (dta104, dta105, etc.). Currently a total of 468 files are created.
  3. If fuzz-testing has been enabled, make will also create fourteen fuzzer targets, one for each of seven file formats, six for internally used grammars, and two fuzzers for testing the compression routines.
    • fuzz_format_dta
    • fuzz_format_por
    • fuzz_format_sas7bcat
    • fuzz_format_sas7bdat
    • fuzz_format_sav
    • fuzz_format_xport
    • fuzz_format_stata_dictionary
    • fuzz_grammar_dta_timestamp
    • fuzz_grammar_por_double
    • fuzz_grammar_sav_date
    • fuzz_grammar_sav_time
    • fuzz_grammar_spss_format
    • fuzz_grammar_xport_format
    • fuzz_compression_sas_rle
    • fuzz_compression_sav

For best results, each sub-directory of the corpus should be passed to the relevant fuzzer, e.g.:

  • ./fuzz_format_dta corpus/dta104
  • ./fuzz_format_dta corpus/dta110
  • ...
  • ./fuzz_format_sav corpus/sav
  • ./fuzz_format_sav corpus/zsav
  • ./fuzz_format_xport corpus/xpt5
  • ./fuzz_format_xport corpus/xpt8

Finally, the compression fuzzers can be invoked without a corpus:

  • ./fuzz_compression_sas_rle
  • ./fuzz_compression_sav

Owner

  • Name: WizardMac
  • Login: WizardMac
  • Kind: organization
  • Location: Chicago, IL

GitHub Events

Total
  • Issues event: 22
  • Watch event: 18
  • Issue comment event: 49
  • Push event: 19
  • Pull request review comment event: 6
  • Pull request review event: 6
  • Pull request event: 9
  • Fork event: 6
  • Create event: 1
Last Year
  • Issues event: 22
  • Watch event: 18
  • Issue comment event: 49
  • Push event: 19
  • Pull request review comment event: 6
  • Pull request review event: 6
  • Pull request event: 9
  • Fork event: 6
  • Create event: 1

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 1,078
  • Total Committers: 32
  • Avg Commits per committer: 33.688
  • Development Distribution Score (DDS): 0.08
Past Year
  • Commits: 22
  • Committers: 6
  • Avg Commits per committer: 3.667
  • Development Distribution Score (DDS): 0.273
Top Committers
Name Email Commits
Evan Miller e****r@g****m 992
David Anthoff a****f@b****u 12
Mikko Marttila m****t@m****m 12
Jari Karppinen j****n@g****m 12
Danny Smith d****y@g****g 6
Lionel Henry l****y@g****m 6
Brian Foley b****y@g****m 5
Kurt Van Dijck d****t@v****e 2
Joris Goosen J****s@J****l 2
Pino Toscano t****o@t****t 2
Slobodan Ilic s****c@g****m 2
hadley h****m@g****m 2
reikoch 8****h 2
zebrys 7****s 2
Bastien b****s@p****m 2
Jérôme Richard j****d@r****m 1
Fajardo, Otto {MDBO~Basel} o****o@r****m 1
Adriaan de Groot g****t@k****g 1
Alex Arslan a****n@c****t 1
Alexander Seiler s****x@g****m 1
Dylan Meysmans c****t@m****m 1
Gábor Csárdi c****r@g****m 1
Ivar Refsdal i****l@n****o 1
Jack 4****c 1
Jonathon Love j****n@t****c 1
Karissa McKelvey k****a 1
Martin Mirchev m****v@s****m 1
Shun Wang s****g@g****m 1
Stefan Gerlach s****h@u****e 1
Timothy Allen f****r@p****m 1
and 2 more...

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 106
  • Total pull requests: 41
  • Average time to close issues: 6 months
  • Average time to close pull requests: about 2 months
  • Total issue authors: 62
  • Total pull request authors: 20
  • Average comments per issue: 2.44
  • Average comments per pull request: 2.63
  • Merged pull requests: 27
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 19
  • Pull requests: 12
  • Average time to close issues: about 2 hours
  • Average time to close pull requests: 21 days
  • Issue authors: 14
  • Pull request authors: 7
  • Average comments per issue: 0.26
  • Average comments per pull request: 1.67
  • Merged pull requests: 6
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • ofajardo (27)
  • evanmiller (5)
  • hadley (4)
  • gorcha (4)
  • khc8749 (3)
  • DanteDT (3)
  • peiyaoli2 (3)
  • vonknio (2)
  • curtisalexander (2)
  • sasutils (1)
  • Bentlerman (1)
  • BERENZ (1)
  • caeu (1)
  • basgys (1)
  • ivelinhristovipsos (1)
Pull Request Authors
  • gorcha (10)
  • evanmiller (6)
  • slobodan-ilic (4)
  • JorisGoosen (2)
  • mettekou (2)
  • gaborcsardi (2)
  • shun2wang (2)
  • FlipperPA (2)
  • seidelma (2)
  • pinotree (2)
  • Marti2203 (2)
  • ankravch (2)
  • kiwiwarmnfuzzy (2)
  • SInginc (2)
  • zebrys (1)
Top Labels
Issue Labels
SAS (27) SPSS (21) bug (19) enhancement (8) Stata (7) question (4) API (3) CLI (2)
Pull Request Labels

Packages

  • Total packages: 4
  • Total downloads:
    • cran 668,687 last-month
  • Total docker downloads: 45,930,804
  • Total dependent packages: 170
    (may contain duplicates)
  • Total dependent repositories: 435
    (may contain duplicates)
  • Total versions: 111
  • Total maintainers: 1
cran.r-project.org: haven

Import and Export 'SPSS', 'Stata' and 'SAS' Files

  • Versions: 24
  • Dependent Packages: 169
  • Dependent Repositories: 435
  • Downloads: 668,687 Last month
  • Docker Downloads: 45,930,804
Rankings
Downloads: 0.4%
Dependent packages count: 0.6%
Dependent repos count: 0.7%
Forks count: 1.0%
Stargazers count: 1.6%
Average: 3.6%
Docker downloads count: 17.3%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/wizardmac/readstat
  • Versions: 42
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.7%
Dependent repos count: 5.9%
Last synced: 6 months ago
proxy.golang.org: github.com/WizardMac/ReadStat
  • Versions: 42
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.7%
Dependent repos count: 5.9%
Last synced: 6 months ago
conda-forge.org: readstat
  • Versions: 3
  • Dependent Packages: 1
  • Dependent Repositories: 0
Rankings
Stargazers count: 22.3%
Forks count: 22.4%
Average: 26.9%
Dependent packages count: 28.8%
Dependent repos count: 34.0%
Last synced: 6 months ago

Dependencies

.github/workflows/build.yml actions
  • actions/checkout v2 composite
  • msys2/setup-msys2 v2 composite
.github/workflows/codecov.yml actions
  • actions/checkout v2 composite
.github/workflows/fuzz.yml actions
  • actions/upload-artifact v1 composite
  • google/oss-fuzz/infra/cifuzz/actions/build_fuzzers master composite
  • google/oss-fuzz/infra/cifuzz/actions/run_fuzzers master composite