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,020

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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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