XPT to CSV Conversion?

16,585

Solution 1

Googling convert "convert sas to csv" turned up this link, which points to a couple of possible solutions.

AM Statistical Software is free statistical software produced by the American Institutes for Research that looks like it can import SAS transport files, and output files in ~150 different formats. I'd guess that .csv is among them!

Solution 2

If you can use Python, I've just published a library that might be able to help with this. Dumping to a CSV would look something like this (untested):

import xport, csv
with xport.XportReader('in.xpt') as reader:
    with open('out.csv', 'rb') as out:
        writer = csv.DictWriter(out, [f['name'] for f in reader.fields])
        for row in reader:
            writer.writerow(row)

The files are treated as streams, so it shouldn't matter how large the file is (as long as you don't call reader.record_count(), which has to seek to the end of the file).

Let me know if you try this -- the library works for me, but I haven't tried it on many .xpt files yet.

Solution 3

Python has xport Library https://pypi.python.org/pypi/xport/ install library

$ pip install xport
$ python -m xport example.xpt > example.csv

Solution 4

I believe there is a read.xport function in one or more packages in R that will read SAS transport files. From there you can use something like write.csv to save it out.

Solution 5

To save readers wasting their time, I'll say that I've just tried AM Statistical Software (version 0.06.04 Beta Aug 14 2011). It accepts an impressive variety of SAS files, but not .xpt .

Share:
16,585

Related videos on Youtube

John Doucette
Author by

John Doucette

Updated on June 04, 2022

Comments

  • John Doucette
    John Doucette almost 2 years

    Perhaps this is the wrong place for this, but I'm not quite sure where to put it.

    I have a very large compressed SAS file in .XPT format. I want to convert it to a comma separated format. The file is too large to load in R. I do not have SAS on my machine, and do not have any way of getting it.

    Any suggestions? Is there a converter somewhere? I cannot find one using google.

  • Josh O'Brien
    Josh O'Brien over 12 years
    read.xport is in package:foreign, but apparently the .XPT file is too large to read directly into R.
  • John Doucette
    John Doucette over 12 years
    Thanks, looks like it will do the trick.
  • Josh O'Brien
    Josh O'Brien over 12 years
    Great. I was glad to learn about it myself. Seems like it might come in handy at some point in the future. Please do make a note, though, if it turns out NOT to work. Thanks.
  • The Red Pea
    The Red Pea over 8 years
    Hi Jack, great library. Is it true it's not compatible with Python 3.*? When I tried to pip install it, and then import it, I got error on a print statement (due to lack of parentheses; required in Python 3.*). I installed it for Python 2.7. Is there a way to tell pip which Python version I have? Is there a way I can edit your library to make compatible with Python 3.*? Thanks!
  • The Red Pea
    The Red Pea over 8 years
    And an TypeError: Oops -- only 8-byte floats are currently implemented. Can't read field {'nfl': 0, 'name': 'DXIHE ', 'nifd': 0, 'niform': ' ', 'nfill': '\x00\x00', 'ntype': 'numeric', 'nfj': 0, 'label': 'Head Imputation Indicator ', 'nform': ' ', 'nvar0': 5, 'nhfun': 0, 'field_length': 3, 'npos': 32, 'num_decimals': 0, 'nifl': 0}. from this file
  • Mike Selik
    Mike Selik over 7 years
    The latest version of xport will work on the command line: python -m xport example.xpt > example.csv
  • jrue
    jrue over 4 years
    This worked perfect for me.
  • Hugo Aboud
    Hugo Aboud about 4 years
    Just tested, AM Statistical Software works, but you need to rename the .XPT file to .V5X