Joakim Hellström
Joakim Hellström

Reputation: 33

Spss .NET Library

I am using the Spss .net library http://spss.codeplex.com to create a .sav file and I cant seem to find how to create cases. I am using C#.

Could someone please point me in the right direction?

Upvotes: 3

Views: 5571

Answers (2)

ranieuwe
ranieuwe

Reputation: 2296

To avoid using the native spssio32/64 and having to deal with SPSS' 32 and 64 bit issues we have created a native implementation of the DLL. It works based on the SPSS/PSPP specification and is a continuation of SpssLib you mentioned before.

An example to write the records as follows:

// Create Variable list
var variables = new List<Variable>
{
    new Variable
    {
        Label = "The variable Label",
        ValueLabels = new Dictionary<double, string>
                {
                    {1, "Label for 1"},
                    {2, "Label for 2"},
                },
        Name = "avariablename_01",
        PrintFormat = new OutputFormat(FormatType.F, 8, 2),
        WriteFormat = new OutputFormat(FormatType.F, 8, 2),
        Type = DataType.Numeric,
        Width = 10,
        MissingValueType = MissingValueType.NoMissingValues
    },
    new Variable
    {
        Label = "Another variable",
        ValueLabels = new Dictionary<double, string>
                    {
                        {1, "this is 1"},
                        {2, "this is 2"},
                    },
        Name = "avariablename_02",
        PrintFormat = new OutputFormat(FormatType.F, 8, 2),
        WriteFormat = new OutputFormat(FormatType.F, 8, 2),
        Type = DataType.Numeric,
        Width = 10,
        MissingValueType = MissingValueType.OneDiscreteMissingValue
    }
};
// Set the one special missing value
variables[1].MissingValues[0] = 999;  

// Default options
var options = new SpssOptions();

using (FileStream fileStream = new FileStream("data.sav", FileMode.Create, FileAccess.Write))
{
    using (var writer = new SpssWriter(fileStream, variables, options))
    {
        // Create and write records
        var newRecord = writer.CreateRecord();
        newRecord[0] = 15d;
        newRecord[1] = 15.5d;
        writer.WriteRecord(newRecord);

        newRecord = writer.CreateRecord();
        newRecord[0] = null;
        newRecord[1] = 200d;
        writer.WriteRecord(newRecord);
        writer.EndFile();
    }
}

Find the source on GitHub and binaries on NuGet.

Upvotes: 4

Timbob
Timbob

Reputation: 135

merthsoft's answer on the following stackoverflow posting provides a good starting point for getting spss up and running. Using 64-Bit SPSS Libraries in C#

The hangups I personally had were including all of the appropriate dlls like... spssio64.dll icudt32.dll icuin32.dll icuuc32.dll

When exporting your data, all columns need to be unique.

If you followed a similar pattern to merthsoft, a possible solution for creating cases may be wrapping and exposing this method...

[DllImport("spssio64.dll", EntryPoint = "spssCommitCaseRecord"...

I hope by now you got it working, but for those who come across this in the future this may help.

Upvotes: 0

Related Questions