Problem to import SPSS/SAS Data

I’m working with SPSS data, but I’m not able to import data. When I import with ReadStat I am not able to convert data to DataFrame. And when I use StatFiles I’m having import error problems. What is the correct way to import data in SPSS format?

using ReadStat;
using DataFrames;
using StatFiles;


Escola=read_sas7bdat("./Datasets/Pisa/Escola/cy6_ms_cmb_sch_qqq.sas7bdat")


df = DataFrame(load("./Datasets/Pisa/Escola/cy6_ms_cmb_sch_qqq.sas7bdat")

Error with StatFiles:


ArgumentError: no default `Tables.columns` implementation for type: StatFiles.StatFile

Stacktrace:
 [1] columns at /Users/alessandrovivas/.julia/packages/Tables/qlc3J/src/fallbacks.jl:156 [inlined]
 [2] DataFrame(::StatFiles.StatFile) at /Users/alessandrovivas/.julia/packages/DataFrames/IKMvt/src/other/tables.jl:21
 [3] top-level scope at In[16]:7

Thanks,

Downloaded example data from here SAS Data files, using julia 1.1:

using ReadStat;
using DataFrames;
d=read_sas7bdat("airline.sas7bdat")
df=DataFrame(d.data,Symbol.(d.labels))

The ReadStatDataFrame struct has the following definition:

mutable struct ReadStatDataFrame
    data::Vector{Any}
    headers::Vector{Symbol}
    types::Vector{DataType}
    labels::Vector{String}
    formats::Vector{String}
    storagewidths::Vector{Csize_t}
    measures::Vector{Cint}
    alignments::Vector{Cint}
    val_label_keys::Vector{String}
    val_label_dict::Dict{String, Dict{Any,String}}
    rows::Int
    columns::Int
    filelabel::String
    timestamp::DateTime
    format::Clong

    ReadStatDataFrame() = 
        new(Any[], Symbol[], DataType[], String[], String[], Csize_t[], Cint[], Cint[],
        String[], Dict{String, Dict{Any,String}}(), 0, 0, "", Dates.unix2datetime(0), 0)
end

From this

For integration with packages like DataFrames.jl you should use the StatFiles.jl package.

I didn’t found the way to do it. Maybe @davidanthoff finds some time to clarify.

1 Like

Or maybe

df=DataFrame(d.data,d.headers)

is better. In the example data the labels are more descriptive, so I used them at first.

1 Like

Thank you! It works!

1 Like