BookmarkSubscribeRSS Feed

How do I write a macro to…split my data set into multiple files?

Started ‎02-06-2015 by
Modified ‎10-05-2015 by
Views 2,037

A very common question on forums and help boards appears to be “How do I write a macro to do XYX?”. Often times a macro isn’t required. This is part one of an ongoing series into how to accomplish a task WITHOUT using a macro.

The first example answers the question of how to Split a data set into multiple files. Any file generated using a file statement can be generated using this method. I will be using the FILEVAR option of a file statement to split the SASHELP.CARS data set into multiple text files, one for each Make. The process and code is below, hope you find it helpful!

This is a two step process:

  1. Sort the file
  2. Generate the output using a Data Step

PROC SORT DATA=SASHELP.CARS OUT=CARS;
BY make;
RUN;

DATA _NULL_;

SET cars; *Dataset to be exported;
BY make; *Variable that file is to be split on;

*Create path to file that is to be exported;
if first.make then out_file=cats(‘/folders/myfolders/’, trim(make));

file temp filevar=out_file dlm=’,’ dsd;

*If first value of make then output column names;
if first.make then
put ‘Make, Model, MPG_HIGHWAY, MPG_CITY';

*Output variables;
put make model mpg_highway mpg_city;

run;

This is a cross post from Statistics and other stuff from a geek

Code is also available here:

StatGeek Gists on GitHub

Version history
Last update:
‎10-05-2015 03:31 PM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags