C # Use SPSS. Net to operate the SPSS Data File

Source: Internet
Author: User

From http://www.lwolf.cn/blog/article/code/Csharp-spss-net.htm

This is something involved in a recent project. We need to import some SPSS data (. Sav) files into the database. Originally, I wanted to save the file as an Excel file in the SPSS software and then operate the Excel file in C. Later, I did not intend to do this. I directly used C # To operate the SPSS data file. So I went to the Internet to find the C # class library for operating SPSS, and found the class library SPSs. net. The current version is 1.0, Which is http://spss codeplex.com /.Source codeAnd direct DLL download.
SPSS. Net actually calls spssio32.dll, which can be found in the installation directory after the SPSS software is installed. I didn't want to install SPSS either. I went to the search to see if spssio32.dll was downloaded. I downloaded a version of SPSS 14 and put it in the System32 directory. Result running exampleProgramSpssio32.dll is not found. Later I saw that it was said that SPSS. NET could only use spssio32.dll of Version 12 or 13. I had to look for it again, but it was fruitless .. Later, I downloaded SPSS 13 and installed it to get the required spssio32.dll.
I am not very familiar with how to use this class library, but I just read its example.CodeIt's from VB. I switched to C #.

Program code: fig. Open ("C: \ test. sav", Fig. Read); // read the SPSS Data File

You can also directly convert files into other data forms.
Program code: Maid ("C: \ test. sav ");

Read the information, variable names, labels, and so on in the variable view of SPSS.
Program code: Maid Doc = maid. Open (txtfilepath. Text, maid. Read );
Foreach (maid VaR in Doc. variables)
{
Console. writeline ("{0} {1}", var. Name, var. Label );
If (VaR is maid)
{
Maid Varnum = (maid) var;
Foreach (dictionaryentry label in Varnum. valuelabels)
{
Console. writeline ("" + label. Key. tostring () + "" + label. value. tostring ());
}
}
}

Read data from the data view
Program code: Maid Doc = maid. Open (txtfilepath. Text, maid. Read );
Foreach (maid VaR in Doc. variables)
{
Console. Write (var. Name + "");
}
Console. writeline ("\ r \ n ");

Foreach (spsscase row in Doc. Cases)
{
Foreach (maid VaR in Doc. variables)
{
If (row [var. Name] = NULL ))
{
Console. Write ("<sysmiss> ");
}
Else
{
Console. Write (row [var. Name]);
}
Console. Write ("");
}
Console. writeline ("\ r \ n ");
}

Create a new SPSS data file and use the maid. Create () method
Program code using (maid Doc = maid. Create ("C: \ example. sav "))
{
// Define the dictionary
Maid ();
V1.name = "V1 ";
V1.label = "What is your name? ";
Doc. variables. Add (V1 );
Maid v2 = new maid ();
V3.name = "V2 ";
V3.label = "What is your gender? ";
V3.valuelabels. Add (1, "male ");
V3.valuelabels. Add (2, "female ");
Doc. variables. Add (V2 );
Doc. commitdictionary ();
// Write data
Spsscase case1 = Doc. Cases. New ();
Case1 ["V1"] = "Andrew ";
Case1 ["V2"] = 1;
Case1.commit ();
Spsscase case2 = Doc. Cases. New ();
Case2 ["V1"] = "Cindy ";
Case2 ["V2"] = 2;
Case2.commit ();
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.