Reading User-Defined SAS Formats and Informats

Goal

You want your application to read user-defined SAS formats and informats.
This recipe applies to the IOM provider. Sample code for ADO is included.
Note: The SAS Workspace Server must have access to the SAS catalog that contains the user-defined formats and informats.

ADO Implementation

Sample Code for Reading User-Defined Formats

The following Visual Basic code shows you how to read a SAS data set named books that uses formats that are contained in a catalog named formats. The data set is located in a directory named c:\storage. The catalog is located in a directory named c:\public.
Note: If you define the libref to point to the location where the catalog is stored, your SAS Workspace Server can access your user-written formats when you process data sets by using either ADO recordsets or OLE DB rowsets.
' obConnection is an open Connection object.
Dim obRecordset As New ADODB.Recordset

' Use the formats persisted on the data set
obRecordset.Properties("SAS Formats") = "_ALL_"

' Assign a libref for the formats catalog.
obConnection.Execute ("libname library 'c:\public' ")

' Assign a libref for the data set.
obConnection.Execute ("libname mylib 'c:\storage' ")

' The second parameter on the Open method must remain empty.
bRecordset.Open "mylib.books", , adOpenDynamic, adLockReadOnly, adCmdTableDirect
Note: Currently, user-written formats cannot be processed on the client side, which is why the IOM provider can process user-written formats but the local, SAS/SHARE, and Base SAS providers cannot. The IOM provider delegates all format processing to the server side. However, the local, SAS/SHARE, and Base SAS providers do all format processing on the client side.
CAUTION:
Do not attempt to open an ADO Recordset object if the underlying data set contains user-written formats that are not immediately available to the IOM workspace.
This action results in a fatal error that is generated even if you are not attempting to use the formats when reading the data.

How to Manage Errors When an Unsupported Provider Is Used

By default, you will get an error if you attempt to use either the local, SAS/SHARE, or Base SAS provider to perform a row I/O operation with a user-written format or informat. For example, if you try to read a row and apply a customized format, the attempt will fail. Likewise, an update or add row operation will fail if a user-written informat is associated with any column in the row.
You can enable processing to continue without error by changing the value of the Recordset property "SAS Format Error" (the OLE DB property name is DBPROP_SAS_FMTERR). This property controls how the local, SAS/SHARE, and Base SAS providers process format and informat names that cannot be resolved.
By default, the value of this property is True, which causes unknown formats and informats to be flagged as errors. If you set this property to False when opening a recordset, the default format or informat (W.D for numeric columns and $CHAR. for character columns) will be applied when an unknown format or informat is encountered.