Friday, August 29, 2008

Dynamically generate SET statement to combine multiple data sets

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.

Note: Variable name lists are not valid for data sets. 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.

/* 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;
run;

Appended results

Obs x
1 1
2 2
3 3

source: www.support.sas.com

0 comments:

Post a Comment

ShareThis