Sample 26010: Dynamically generate SET statement to combine multiple data sets
You can manually enter the data set names or use MACRO logic to generate the repetitive data set names when combining many data sets on a SET statement. Starting in SAS 9.2, dash lists can be used to list out the data sets.
Note:
Variable name lists are not valid for data sets before SAS 9.2. In other words, if you have WORK.DS1, WORK.DS2, and WORK.DS3, you can NOT specify WORK.DS1-WORK.DS3 on the SET statement.
/* Create sample data sets to append with the macro. Note the data set */
/* names use the naming convention of DSn, where n is an incrementing */
/* number. */
data ds1;
x=1;
run;
data ds2;
x=2;
run;
data ds3;
x=3;
run;
/* Build a macro called NAMES with two parameters. The first parameter */
/* is the 'prefix' of the naming pattern. The second parameter is the */
/* maximum number of data sets you want to generate on the SET statement. */
%macro names(prefix,maxnum);
%do i=1 %to &maxnum;
&prefix&i
%end;
;
%mend names;
/* Call the macro on the SET statement */
data all;
set %names(DS,3);
run;
proc print data=all;
title "Appended results";
run;
Starting in SAS 9.2, a dash list can be used.
data all;
set ds1-ds3;
run;
Appended results
Obs x
1 1
2 2
3 3
Use MACRO logic to populate a SET statement to combine many data sets.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> SET Common Programming Tasks ==> Combining Data
|
| Date Modified: | 2008-01-28 11:45:43 |
| Date Created: | 2006-01-17 17:59:43 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |