Turn your data set into a DATA step program

Sometimes I need to "disassemble" a SAS data set into a DATA step program. It's kind of like creating a "freeze-dried" version of the data that you can carry around and use anywhere, re-hydrating it in the SAS session where you next need it.

Some example uses for this:

Build standalone program examples that include the data "inline", so that you don't have to supply a SAS7BDAT file and a SAS program file together to make it work.

Create a portable version of the data that you can transport from one SAS environment and use easily on another environment, without having to worry about platform encoding differences. (Of course, you can also use PROC MIGRATE for this job.)

Capture the structure of the data, with the columns, formats, labels, and lengths -- even if you don't need all of the records.

There are various programming approaches that you can use to accomplish this in SAS, but to make it easier I built a custom task called Data set->DATA step. (The task runs in SAS Enterprise Guide 4.3 and later.) This task reads the contents of the active data set, generates a DATA step program with the appropriate ATTRIB statements for each column, and then includes all of the data records as DATALINES.

This screen shows the task user interface, which gives you a preview of the DATA step setup. If you want just the data structure portion of the program, you can click Copy to clipboard and thus capture just that piece.

When you click Run, the task builds a program that contains the DATA step along with all of the DATALINES records. It doesn't actually run the program, since that would only recreate a copy of the data set that you already have, but it does produce a short summary log for output. And the contents of the Code tab in the results will contain the complete DATA step program.

(If you're wondering how I'm "splitting" the code view in this picture, check out this blog post about an editor trick.)

You can download the task from the list of custom task examples that accompany my recent book. The "Data set->DATA step" example is featured in "Chapter 12: Abracadabra: Turn Your Data into a SAS Program". You are not required to own the book in order to use the examples (but I promise that you can learn a lot from the book).

If you're interested in how this task does its work, here are the techniques used in the source code and described within the chapter:

Use ADO.NET to connect to and read SAS data. (ADO.NET is the method for working with data sources in .NET programs.)

Use SAS DICTIONARY tables to discover data set and column attributes for the source data. Then, use that information to influence task behavior.

Use the SASTextEditorCtl control from the SAS.Tasks.Toolkit library to preview the SAS program in the SAS color-coded Program Editor.

Encapsulate the meat of the task -- the business logic that reads data and creates a SAS program -- into a separate .NET class. This makes the task more maintainable and enables you to reuse the business logic in other contexts.

Use a special task API interface, named ISASTaskExecution, to implement the work of the task. This enables SAS Enterprise Guide to delegate all task processing to your task so that it can perform work that can't be done easily in a SAS program.

More resources:

Sample chapter from Custom Tasks for SAS Enterprise Guide using Microsoft .NET, which includes instructions for how to install (deploy) custom tasks.

Example custom task projects, including complete source code.