user8270077
user8270077

Reputation: 5071

Trying to import a sav file with pd.read_sas() method fails

I am trying to import as a pandas dataframe a sav file. The origin of the file is the Pew Research Center (http://www.pewglobal.org) and is publicly available.

My code is the following:

import pandas as pd

data = pd.read_sas('Pew_Research_Global_Attitudes_Spring_2017_Dataset_WEB_FINAL.sav')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-d54b05eebfbc> in <module>()
----> 1 data = pd.read_sas('Pew_Research_Global_Attitudes_Spring_2017_Dataset_WEB_FINAL.sav')

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\sas\sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
     50             pass
     51 
---> 52     if format.lower() == 'xport':
     53         from pandas.io.sas.sas_xport import XportReader
     54         reader = XportReader(filepath_or_buffer, index=index,

AttributeError: 'NoneType' object has no attribute 'lower'

Upvotes: 0

Views: 700

Answers (1)

Flow
Flow

Reputation: 791

You are trying to read a SPSS .sav file with a function made to read SAS .xpt or .sas7bdat files. That is why it does not work.

Per Pew Research Center, you will need some additional steps to use .sav file with something else as SPSS. A good starting point can be to convert it to csv:

SPSS .sav files can be converted into .csv format using R. R is a free, open source program for statistical analysis that can be downloaded from the Comprehensive R Archive Network. The foreign package is part of the standard installation, and can be used to read .sav files into R with the read.spss() function. The data can then either be analyzed in R or saved to a .csv file using the write.csv() function.

Source

Upvotes: 1

Related Questions