How to read a SAS dataset with C#

It’s a bit like playing the guitar: easy when you know how! It took me a little while today, though, as the SAS samples weren’t very clear. What was missing was primarily how to fully specify the SAS library _and_ dataset programmatically.

I had to combine two descriptions: this and this.


OleDbConnection cn = new OleDbConnection();
cn.ConnectionString =
"Provider=sas.IOMProvider; Data Source=_LOCAL_";
cn.Open();

Console.WriteLine("SAS Server Version: " + cn.ServerVersion);
// Create an OleDbCommand object and tell it            
// 1) to assign the library
OleDbCommand sascmd = cn.CreateCommand();
sascmd.CommandType = CommandType.Text;
sascmd.CommandText =
@"libname cslib 'D:\SAS_RDB\SAS_DATA\v8\1996\'";
sascmd.ExecuteNonQuery();

// 2) to open the specified table.
sascmd.CommandType = CommandType.TableDirect;
sascmd.CommandText =
"cslib.aregn96";
// Execute the command and get an OleDbDataReader object.
OleDbDataReader reader = scmd.ExecuteReader();

As you see the trick is to assign the LIBRARY reference programatically.

Advertisements
Posted in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s