Sample 48810: Append all SAS data sets in a SAS library into 1 SAS data set when the number of SAS data sets varies
Assume that today the SAS library has 3 SAS data sets that you want to concatenate and create 1 SAS data set. The first step is to get a listing of the SAS data sets using PROC CONTENTS. Remove the duplicate names of the SAS data sets with PROC SORT. Next, a DATA _NULL_ step was used to obtain a count of the number of SAS data sets, and place the names of the SAS data sets into macro variables. Then, a macro containing a PROC APPEND nested inside a macro DO loop is used to append the SAS data sets into 1 SAS data set.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Libname to allocate the SAS data library to be read - you will use your SAS data library
instead of c:\temp */
libname abc 'c:\temp';
/* Run a PROC CONTENTS to create a SAS data set with the names of the SAS data sets in the SAS data
library */
proc contents data=abc._all_ out=abccont(keep=memname) noprint;
run;
/* Eliminate any duplicate names of the SAS data set names stored in the SAS data set */
proc sort data=abccont nodupkey;
by memname;
run;
/* Run a DATA _NULL_ step to create 2 macro variables - one with the names of each SAS data set
and the other with the final count of the number of SAS data sets */
data _null_;
set abccont end=last;
by memname;
i+1;
call symputx('name'||trim(left(put(i,8.))),memname);
if last then call symputx('count',i);
run;
/* Macro containing the PROC APPEND that executes for each SAS data set you want to concatenate
together to create 1 SAS data set */
%macro combdsets;
%do i=1 %to &count;
proc append base=new data=abc.&&name&i force;
run;
%end;
%mend combdsets;
/* Execute the macro */
%combdsets
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> APPEND SAS Reference ==> Macro
|
Date Modified: | 2013-01-03 09:45:13 |
Date Created: | 2012-12-21 13:57:38 |
Operating System and Release Information
SAS System | Base SAS | z/OS | | |
Z64 | | |
OpenVMS VAX | | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
Microsoft Windows XP 64-bit Edition | | |
Microsoft® Windows® for x64 | | |
OS/2 | | |
Microsoft Windows 8 Pro | | |
Microsoft Windows 95/98 | | |
Microsoft Windows 2000 Advanced Server | | |
Microsoft Windows 2000 Datacenter Server | | |
Microsoft Windows 2000 Server | | |
Microsoft Windows 2000 Professional | | |
Microsoft Windows NT Workstation | | |
Microsoft Windows Server 2003 Datacenter Edition | | |
Microsoft Windows Server 2003 Enterprise Edition | | |
Microsoft Windows Server 2003 Standard Edition | | |
Microsoft Windows Server 2003 for x64 | | |
Microsoft Windows Server 2008 | | |
Microsoft Windows Server 2008 for x64 | | |
Microsoft Windows Server 2012 | | |
Microsoft Windows XP Professional | | |
Windows 7 Enterprise 32 bit | | |
Windows 7 Enterprise x64 | | |
Windows 7 Home Premium 32 bit | | |
Windows 7 Home Premium x64 | | |
Windows 7 Professional 32 bit | | |
Windows 7 Professional x64 | | |
Windows 7 Ultimate 32 bit | | |
Windows 7 Ultimate x64 | | |
Windows Millennium Edition (Me) | | |
Windows Vista | | |
Windows Vista for x64 | | |
64-bit Enabled AIX | | |
64-bit Enabled HP-UX | | |
64-bit Enabled Solaris | | |
ABI+ for Intel Architecture | | |
AIX | | |
HP-UX | | |
HP-UX IPF | | |
IRIX | | |
Linux | | |
Linux for x64 | | |
Linux on Itanium | | |
OpenVMS Alpha | | |
OpenVMS on HP Integrity | | |
Solaris | | |
Solaris for x64 | | |
Tru64 UNIX | | |