BookmarkSubscribeRSS Feed

From DATA Step to DS2: Converting Your Code with PROC DSTODS2

Started 2 weeks ago by
Modified 2 weeks ago by
Views 100

In my last article, I introduced DS2, SAS's language for advanced data manipulation, and promised a closer look at how it relates to the DATA step. Thankfully, if you’re a long-time SAS programmer who wants to take advantage of of DS2's precision, reusability, and speed, converting a DATA step program to DS2 doesn’t mean manually rewriting everything by hand.

 

In this article, I'll discuss PROC DSTODS2, a procedure that reads a file containing a DATA step program and automatically generates the equivalent DS2 data block for you, including how it works and what to expect (and fix) once your code comes out the other side.

 

 

How PROC DSTODS2 Works

 

PROC DSTODS2 has three options:

 

  • IN= specifies the location of the file containing the DATA step program you want to convert.
  • OUT= specifies the name of the file that will contain the generated DS2 code.
  • OUTDIR= specifies the directory where the output file should be written.

 

Note: OUTDIR= requires a trailing path delimiter. Without it, your converted file may end up in a location you didn't intend.

 

If your DATA step program contains statements that PROC DSTODS2 can't convert, it places them as comments in the output file, so you can review and manually adapt anything that didn't translate automatically.

 

By default, if you don't specify OUTDIR=, the converted program is saved to the SAS session's default temporary location (something like /opt/sas/viya/config/var/temp/...). That path is hard to navigate to and not very practical to work with, so in most cases you'll want to set OUTDIR= explicitly to somewhere more useful, like a folder in your SAS server files.

 

 

Walking Through a Conversion

 

Let's look at an example. The following DATA step program reads a copy of sashelp.cars in the WORK library and uses IF-THEN/ELSE logic to route each row to one of three output tables — usa, europe, or asia — depending on where the car originated.

 

data usa europe asia;
   set sashelp.cars;
   if Origin = 'USA' then do;
      output usa;
   end;
   else if Origin = 'Europe' then do;
      output europe;
   end;
   else do;
      output asia;
   end;
run;

 

Running this DATA step creates three new tables, as shown in the log:

 

treiman_DataDS2_01.png

 

To convert it to DS2, we can set up a FILENAME statement pointing to the DATA step program, then run PROC DSTODS2:

 

filename myds2 src '/Users/Location';

proc dstods2
    in=myds2
    out='DS2.sas'
    outdir='path/to/output/folder/';
run;

 

This creates a converted version of the program called DS2.sas in the specified output directory.

 

The output of PROC DSTODS2 looks like this:

 

treiman_DataDS2_02.png

 

 

Cleaning up the Converted Code

 

The results contain a DS2 data block, but it's missing some of the scaffolding a working DS2 program needs, and it's not well-formatted. Some of the things we’ll need to fix include:

 

  • Missing PROC DS2 statement. You may also need to add the OVERWRITE=YES option since DS2 does not overwrite existing tables by default.
  • Missing RUN and QUIT statements.
  • Reserved words in quotes. If any of the DATA step's data set names happen to be reserved words in DS2, PROC DSTODS2 will automatically wrap them in double quotation marks.
  • Leftover null statements and unused labels. You may see a stray semicolon (a null statement) and an unused label like _return_: near the end of the program. These can be deleted.
  • Formatting. The generated code isn't formatted cleanly. We can use the Format Code button in SAS Data and AI studio to fix this.
  • Declare new variables. In this example, it doesn't apply, but since DS2 requires newly created variables to be actively declared, you may need to add DECLARE statements when converting DATA step to DS2.

 

The cleaned-up code is as follows:

 

proc ds2;
    data USA EUROPE ASIA / overwrite=yes;
        method run();
            set WORK.CARS;
            if ORIGIN='USA' then do ;
                output USA;
            end;
            else if ORIGIN='Europe' then do ;
                output EUROPE;
            end;
            else do ;
                output ASIA;
            end;
        end;
    enddata;
    run;
quit;

 

Running the cleaned-up program will generate the same three output tables, with exactly the same results as the original DATA step.

 

 

Conclusion

 

PROC DSTODS2 won't magically turn your DATA step programs into production-ready DS2 code, but it does the heavy lifting of translating syntax so you're not starting from a blank page. Expect to add the PROC DS2/RUN/QUIT wrapper, clean up formatting, and declare any new variables being created. However, once you've made those adjustments and verified your results, you'll have a working DS2 program that generates exactly the same results!

 

 

Find more articles from SAS Global Enablement and Learning here.

Contributors
Version history
Last update:
2 weeks ago
Updated by:

Viya Copilot Motion Graphic.gifViya Copilot Motion Graphic

Ready to see what SAS Viya Copilot can do?

Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.

Get Started →

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags