Listing the data sets in a library within your custom task

A SAS Professionals attendee and Twitter follower named Marco asks for help:

..struggling to find a method with custom tasks in EG to be able to list the datasets in a library, can you help please?

Sure, no problem. This is easy-peasy-lemon-squeezy.

First, make sure that you have a reference to the SAS.Tasks.Toolkit.dll in your custom task project. If you used the Visual Studio template to set it up, you've got it.

The SAS task toolkit library provides many easy-to-use classes to help your custom tasks deal with SAS servers, libraries, and data, including:

  • SAS.Tasks.Toolkit.SasServer
    Represents the basic attributes of a SAS server, including its name, host, and connectivity status.
  • SAS.Tasks.Toolkit.Data.SasLibrary
    Represents a SAS library including its descriptive name (from metadata), libref, library engine, and more.
  • SAS.Tasks.Toolkit.SasServer.SasData
    Represents a SAS data set and provides access to data set attributes and columns.
  • SAS.Tasks.Toolkit.SasServer.SasColumn
    Represents a SAS column and its attributes (name, type, format, and more).

To accomplish what Marco is looking to do, the C# code looks something like:

using SAS.Tasks.Toolkit.Data;
// Get a library object
SasLibrary lib = new SasLibrary("Local", "SASHELP");
System.Diagnostics.Debug.WriteLine(string.Format("Data members in: {0}", lib.Name));
// list each data set name
foreach (SasData d in lib.GetSasDataMembers())
    System.Diagnostics.Debug.WriteLine(string.Format(" Data member: {0}", d.Member));

The output looks like this:

Data members in: SASHELP
 Data member: _CMPIDX_
 Data member: ADOMSG
 Data member: ADSMSG
 Data member: AFMSG
 Data member: AIR
 Data member: ASSCMGR
 Data member: BMT

Many methods return collections of objects (such as SAS.Tasks.Toolkit.Data.SasLibrary.GetSasDataMembers) that support the enumerating interfaces to enable data binding, making it easy to show a list of data sets or columns in a list view without cumbersome coding.

For example, this snippet puts the data set names into a Windows listbox control:

// lbData is System.Windows.Forms.ListBox
lbData.DisplayMember = "Member";
lbData.DataSource = lib.GetSasDataMembers();
tags: .net, SAS custom tasks, SAS Enterprise Guide


  1. Marco Oliver
    Posted July 17, 2012 at 5:31 am | Permalink

    Hi Chris,

    Thank you for your help. We had explored this method but as I should have probably said on my initial tweet (but was limited by the character allowance) we are using Enterprise Guide Version 4.1. Unfortunately this does make it more difficult for us as we cannot make use of the Toolkit. For anyone using 4.2 and above your solution will be great and should save a lot of time and effort. I have found a work around by calling a SAS macro from within the custom task, which utilises the Vcolumn dataset and then reads the values back to the custom task.

    I was wondering if you knew of anyway in 4.1, possibly using the Workspace Server that I could achieve the same results. As I said I have already implemented the work around but if I can use an alternative, more efficient method that would be great. Unfortunately, I cannot seem to find any documentation that would suggest I can achieve this.

    Thank you for all your help.


    • Chris Hemedinger Chris Hemedinger
      Posted July 17, 2012 at 4:27 pm | Permalink


      It might be easier for you to use the SAS OLE DB IOM data provider to read the contents of VCOLUMN and other "metadata" dictionary tables. There are examples -- 4.1 compatible -- in the SAS Catalog Explorer within these source files. Specifically, look at CatalogExplorer\CatalogExplorerForm.cs.

      • Marco Oliver
        Posted July 18, 2012 at 3:12 am | Permalink

        Thank you for your help Chris. It is much appreciated.

One Trackback

  1. [...] I described in a previous post, I'm using data binding to populate the UI controls with the content of the SAS data [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>