Skip to Main Content

Combining Multiple SAS Files Using a Macro with the “set” command | SAS Code Fragments

If you had 2 SAS data files, say "file1" and "file2", you
could combine them using a program like…

data both;
set file1 file2 ;
run;

If, however, you had 200 SAS data files, say "file1"
"file2" … "file200" then this would be alot of typing
doing the "set" statement for 200 files.  Instead, you can make a
SAS macro as shown below.  This macro just combines "file1" and
"file2", but can be extended to read multiple files.

data file1;
input a;
cards;
1
2
3
;
run;

data file2;
input a;
cards;
4
5
6
;
run;

%macro combine;

data big;
set
%do i = 1 %to 2;
file&i
%end;
;
run;

%mend;

%combine;

proc print data=big;
run;