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.
PROC DSTODS2 has three options:
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.
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:
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:
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:
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.
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.
Visit the Tips & Tricks page for setup guidance, demos, and practical examples that show how Copilot supports your workflows.
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.