Thursday, February 17, 2011

SAS Transport Files and .NET

Well, someone contacted me about the Data Management Utilities. they loved them (thank you) but wanted to know about reading SAS Transport Files. Well, after fiddling with the localProvider a bit and getting nowhere, I stumbled onto something cool:

  • Download and install the SAS Universal Viewer from the SAS support site
  • Create a new project in Visual Studio
  • Add a reference from the SAS Universal Viewer install files to the following 2 dlls
  •    SAS.UV.Transport
  •    SAS.UV.Utility
  • Use the following C# code (adapt as needed):
TransportFile tf = new TransportFile(@"x:\temp\sample.xpt");
var x = tf.Datasets;

Your dataset will be in variable x

UPDATED: 11/8/2017

[NOTE: I have also created a direct reader for the XPT binary format. That took a long time (a week, maybe). Contact me if you need that]

Complete code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAS.UV.Transport;

namespace ReadSasXportFiles
    class Program

        static void Main(string[] args)
            var tf = new TransportFile(@"x:\temp\shoes.xpt");
            var x = tf.Datasets;

IMPORTANT NOTE: Make sure your build is set for x64 and not Any CPU. You will see a warning:

Warning There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "SAS.UV.Transport", "AMD64".

No comments:

SAS throwing RPC error

If you are doing code in C#  and get this error when creating a LanguageService: The RPC server is unavailable. (Exception from HRESULT:...