6

I need some help... I got some troubles reading my sas table in python using the pandas function read_sas. I got the following error:

"ValueError: Length of values does not match length of index".

Here is the code I run:

import pandas as pd

data=pd.read_sas("my_table.sas7bdat")
data.head()

My sas table is pretty big with 505 columns and 100 000 rows.

Thanks all for your help.

RagNar
  • 61
  • 1
  • 2
  • Hmm. Maybe try reading the file in increments? (Set chunksize and iterator variables. ) See more on https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sas.html – pinegulf Jul 11 '18 at 09:44
  • Thanks for your answer but it does not fix the problem. I found a solution which is to convert my table to csv and use pd.read_csv to read it. – RagNar Jul 11 '18 at 14:10
  • Make sure that the dataset does not have numeric variables defined with a length of less than 8 bytes. Current release of read_sas does not handle those properly. – Tom Jul 11 '18 at 22:38

2 Answers2

4

I had the same problem with few sas files. I solved it in 2 ways: 1. encoding

df=pd.read_csv('foo.sas7bdat.csv', encoding='iso-8859-1')

2. With sas7bdat library installed in Anaconda with:

conda install -c prometeia/label/pytho sas7bdat

In python file:

from sas7bdat import SAS7BDAT
f=SAS7BDAT('foo.sas7bdat').to_data_frame()
Naomi Fridman
  • 2,095
  • 2
  • 25
  • 36
  • Thanks. I might suggest splitting these off into two answers. The `encoding` approach failed for me, but `sas7bdat` worked. – Max Ghenis Oct 25 '21 at 15:43
0

A solution I found is to export my sas table as a csv file with the code down below:

proc export data=my_table
   outfile='c:\myfiles\my_table.csv'
   dbms=csv
   replace;
run;

After that, I use pandas function read_csv to read the csv file I have just created:

import pandas as pd

data=pd.read_csv("my_table.csv")

data.head()

Hope this might help.

RagNar
  • 61
  • 1
  • 2
  • 1
    Did you also manage to find a solution if you don't have the option to export to csv in SAS ? – stedes Aug 21 '18 at 07:13
  • I only tried to export in csv format sorry... Maybe you can try to export your SAS table in txt format and read it with pandas.read_table() – RagNar Aug 23 '18 at 12:43