Saturday, June 22, 2013

Macro: Load and Append Split Files

Sometimes it's necessary to split data files up. Maybe there's additional data points rolling in as time passes, or you're sending the data to a contractor and the file sizes can't be too big. When receiving these files, it's useful to use the APPEND procedure to put them back together.

I recently had a set of five or so such files, so instead of loading each individually, I built a macro to load, append, load, append, etc.

It's not a plug 'n play macro, since adjusting the variables, formats, and base_data_set are required, but here's the skeleton of it:

/*
Load a series of files, and append them to the base dataset.
*/

%macro append(fname);
data temp;
infile &fname dsd truncover;
input 
var1 format1.
var2 format2.; ;
run;

proc append base=base_data_set data=temp;
run

%mend append;

/*
Load and append 5 files..
*/
%append("file1.csv");
%append("file2.csv");
%append("file3.csv");
%append("file4.csv");
%append("file5.csv");

This is a lot cleaner than writing a separate data step and append proc for each new file!

No comments:

Post a Comment