XPT to CSV Conversion?

John Doucette picture John Doucette · Oct 10, 2011 · Viewed 13.6k times · Source

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.

Answer

Jack Cushman picture Jack Cushman · May 2, 2012

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.