Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPT to CSV Conversion? [closed]

Tags:

r

csv

sas

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.

like image 988
John Doucette Avatar asked Oct 10 '11 18:10

John Doucette


2 Answers

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

$ pip install xport
$ python -m xport example.xpt > example.csv
like image 71
Rishikesh Teke Avatar answered Nov 15 '22 23:11

Rishikesh Teke


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.

like image 37
Jack Cushman Avatar answered Nov 15 '22 22:11

Jack Cushman