Listing the data sets in a library within your custom task

4

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();
Share

About Author

Chris Hemedinger

Senior Manager, SAS Online Communities

+Chris Hemedinger is the manager of SAS Online Communities. He's also co-author of the popular SAS for Dummies book, author of Custom Tasks for SAS Enterprise Guide using Microsoft .NET, and a frequent participant on the SAS Enterprise Guide discussion forum.

Related Posts

4 Comments

  1. Marco Oliver on

    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.

    Marco

  2. Pingback: Object-oriented access to SAS data in a custom task - The SAS Dummy

Leave A Reply

Back to Top