Specifying a Libref to Use with the IOM Provider

Goal

You want to use the IOM provider to open or create a table, so you need to know how to specify (assign) a libref.
This recipe applies to the IOM provider. Sample code for ADO is included.

Implementation

Two Methods for Assigning a Libref

SAS organizes tables in libraries. Before you can use a SAS library, you must tell SAS where it is. One way to identify the library is to use a libref, which is a short name (or alias) for the full physical name of the library. Here are two ways that you can assign a libref:
  • You can use ADO to assign a libref by executing a LIBNAME statement through an ADO Command object.
  • You can use the IOM DataService class to assign a libref.
Both techniques assign a libref for the duration of the SAS session or until the libref is unassigned. When choosing between the two methods, consider the information in the following table:
How to Choose Between the Two Methods
Your Situation or Preference
Recommended Method
You are comfortable using the SAS LIBNAME statement.
Use ADO.
You are not using SAS objects to connect to your server.
Use ADO.
You are using SAS objects to connect to your server.
Use the IOM DataService class.
You want to know whether the libref was successfully assigned.
Use the IOM DataService class (because the AssignLibref method has a return code).
You want to take advantage of other methods and attributes on the Libref object.
Use the IOM DataService class.

Using ADO to Assign a Libref

You can use ADO to assign a libref by executing a LIBNAME statement through an ADO Command object. The following Visual Basic code shows how this task is done.
' obConnection is an open Connection object.
Dim obRecordset As New ADODB.Recordset
Dim obCommand As New ADODB.Command

obCommand.ActiveConnection = obConnection 

'Assign a libref by executing a LIBNAME statement.
obCommand.CommandType = adCmdText
obCommand.CommandText = "libname mylib 'c:\census\data'"
obCommand.Execute

'Open a data set in the assigned library.
obRecordset.Open "mylib.cities", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect 

Using the IOM DataService Class to Assign a Libref

You can use the IOM DataService class to assign a libref. The following Visual Basic code shows how this task is done.
Note: To use the sample code, you must reference these libraries in your Visual Basic project: SAS Integrated Object Model (IOM) Type Library and the SASObjectManager Type Library. These libraries are installed with SAS Integration Technologies.
Dim obObjectFactory As New SASObjectManager.ObjectFactory 

Dim obConnection As New ADODB.Connection
Dim obRecordset As New ADODB.Recordset

Dim obSAS As SAS.Workspace
Dim obLibRef As SAS.Libref

' Use the SAS Object Manager to establish a SAS workspace object.
set obSAS = obObjectFactory.CreateObjectByServer("MyServer", True, Nothing, "", "")

Dim obObjectKeeper As New SASObjectManager.ObjectKeeper 

obObjectKeeper.AddObject(1, "MyServer", obSAS) 

' Call the AssignLibref method in order to assign a libref.
Set obLibRef = obSAS.DataService.AssignLibref("mylib", "", "c:\census\data", "")

' Open a connection.
obConnection.Open "Provider=sas.IOMProvider;SAS Workspace ID=" & obSAS.UniqueIdentifier

' Open a data set in the assigned library.
obRecordset.Open "mylib.cities", obConnection, adOpenStatic, adLockReadOnly, adCmdTableDirect
In the sample code, obSAS, obLibRef, and obObjectFactory are all IOM objects. For more information about the SAS object hierarchy, see the SAS Integration Technologies: Windows Client Developer's Guide.