Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spss .NET Library

Tags:

c#

.net

spss

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?

like image 734
Joakim Hellström Avatar asked Jun 15 '26 15:06

Joakim Hellström


1 Answers

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.

like image 67
ranieuwe Avatar answered Jun 17 '26 05:06

ranieuwe