We’re smarter together. Learn from this collection of community knowledge and add your expertise.

How to create a data step version of your data AKA generate sample data for forums

by Super User on ‎03-24-2016 09:51 AM (7,848 Views)

When asking question on the forum, it's incredibly helpful to provide sample data. Sometimes you can get away with referencing SASHELP datasets, but it's also nice to be able to use your own data, especially if you have a complex structure or just code that will work on your computer. 


Mark Jordan has written a SAS macro that will generate a data step version of your data. This post goes through how to use it on SAS University Edition. You add the code to your autoexec so that it's available everytime you start up SAS UE.  


The macro call is:

%data2datastep(dsn, lib, file, obs);


dsn = input data set name, required


lib = library where input data set resides, optional


file = name of output file, optional


obs = number of obs to include in the data step, optional 

Here's how to get it installed and working in SAS UE.


1. Download the zip file from Mark's blog: http://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/

2. Extract the zip file and place the data2datastep.sas program into your myfolders folder set up.

Screen Shot 2016-03-23 at 8.03.29 PM.png

3. In SAS University Edition, go to the Edit the AutoExec file to add the program to your autoexec

Screen Shot 2016-03-23 at 8.04.29 PM.png


4. In the Autoexec, add the reference to the program, and RUN, then SAVE.


%include '/folders/myfolders/data2datastep.sas' /lrecl=1000;

Screen Shot 2016-03-23 at 8.04.44 PM.png



5. Test the code in SAS UE



6. A file is generated called create_sashelp_CARS_data.sas


  infile datalines dsd truncover;
  input Make:$13. Model:$40. Type:$8. Origin:$6. DriveTrain:$5. MSRP:DOLLAR8. Invoice:DOLLAR8. EngineSize:32. Cylinders:32. Horsepower:32. MPG_City:32. MPG_Highway:32. Weight:32. Wheelbase:32. Length:32.;
Acura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820","$21,761",2,4,200,24,31,2778,101,172
Acura,TSX 4dr,Sedan,Asia,Front,"$26,990","$24,647",2.4,4,200,22,29,3230,105,183
Acura,TL 4dr,Sedan,Asia,Front,"$33,195","$30,299",3.2,6,270,20,28,3575,108,186
Acura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755","$39,014",3.5,6,225,18,24,3880,115,197

Voila! You have data to post in the forum!


by Valued Guide
on ‎12-20-2016 12:04 PM
This tagset will create SQL insert code to recreate the SAS dataset.

However a bug was introduced in 9.4 where the length of character variables is missing ie varchar(). It used to work.

filename tagset http "http://support.sas.com/rnd/base/ods/odsmarkup/sql.sas";
   %include tagset;
   ods tagsets.sql file="d:/sd1/class.sql";
   proc print data=sashelp.class ;
   ods _all_ close;
   ods listing;
by PROC Star
on ‎02-13-2018 09:54 PM


I think you need to replace

set $col_def $col_def "(" width ")" /if $types_with_widths[type];


set $col_def $col_def "(" colwidth ")" /if $types_with_widths[type];

I asked SAS to correct.

Your turn
Sign In!

Want to write an article? Sign in with your profile.

Looking for the Ask the Expert series? Find it in its new home: communities.sas.com/askexpert.