The attachment works when the type of variables across all datasets is same. If not, the code must be modified to handle this mismatch, and errors will occur while the code is running.
Please update the rows about the 'libname' as you need.
Edit by KB:
The code in a code window, so nobody needs to view/download attachments:
libname folder1 "path_to_folder1";
libname folder2 "path_to_folder2";
libname folder3 "path_to_folder3";
libname folder4 "path_to_folder4";
libname placed "path_to_you_wanna_place_dataset";
options validvarname=upcase;
/*get the informations about all datasets of all folder*/
proc sql noprint;
create table vcolumn as
select distinct libname,memname,name,type,length from sashelp.vcolumn
where libname like 'FOLDER%' and memtype='DATA' ;
quit;
/*get properties of all unique variables*/
proc sql noprint;
create table vcol as
select distinct memname,name,type,max(length) as length
from vcolumn group by memname,name;
quit;
proc sort;by memname name type;run;
data attributes_var;
length attr $10000 attr_var $200;
retain attr;
set vcol;
by memname name type;
if type in ('char') then attr_var=catx(' ',name,cats('char(',put(length,best.),')'));
else attr_var=catx(' ',name,'num');
if first.memname then attr=strip(attr_var);
else attr=catx(' , ',attr,attr_var);
if last.memname then attr=cats('(',attr,');');
if last.memname then output;
run;
/*get datasets to set*/
proc sql noprint;
create table vname as
select distinct memname,libname from vcolumn;
quit;
proc sort;by memname libname;run;
data attributes_dataset;
length attr $10000 attr_dataset $200;
retain attr;
set vname;
by memname libname;
attr_dataset=catx('.',libname,memname);
if first.memname then attr=strip(attr_dataset);
else attr=catx(' ',attr,attr_dataset);
if last.memname then output;
run;
proc sql noprint;
select count(distinct memname) into: nloop trimmed from attributes_dataset;
quit;
%put &nloop.;
%macro loop(placed_folder=);
%do i=1 %to &nloop.;
data _null_;
set attributes_dataset;
if _n_=&i.;
call symputx('name',strip(memname),'G');
call symputx('datasets',strip(attr),'G');
run;
proc sql noprint;
select distinct strip(attr) into: vars
from attributes_var where memname in ("&name.");
quit;
%put &i./&nloop.==>&name. &datasets.<==>&vars.;
proc sql noprint;
create table temp
&vars.;
quit;
data &placed_folder..&name.;
set temp &datasets.;
run;
%end;
%mend loop;
/*the arguement of 'placed_folder' is the libname to place the combined dataset*/
%loop(placed_folder=placed);
... View more