Hi, I wanna combine hundreds of *.sas7bdat in one folder on Windows Platform. The names of datasets have no rule. How can I do put them together using data steps?
Thank you for your time.
Message was edited by: Jun
Thank you for your reply, Linus.
The hundreds of datasets(*.sas7bdat) have same structure and are placed in same folder.
Could you give me some code to merge them vertically?
Data step is preferred, and PROC SQL is OK too.
To concatenate tables together (or, in your terms, to join them vertically (as opposed to merging them), you could use either PROC APPEND or a SET statement in a DATA step.
So, if you had these two tables (for example) and they both had the same variables NAME and AGE, then you could contenate them together:
...with this program
would result in NEWTABLE containing:
You could also use PROC APPEND, or PROC SQL.
If you want the list of DATASET names for the SET to be populated dynamically, then you would have to follow Linus' suggestion and investigate the use of DICTIONARY.TABLES and some simple SAS Macro processing (the use of INTO within an SQL query) to give you the names of the tables that you want to concatenate together.
If you know about SAS Macro processing, then the program below should be fairly simple to follow. If you don't know about SAS Macro processing, then I suggest that you investigate and learn about the SAS Macro facility before implementing a production solution.
The program below creates 3 datasets (copies of SASHELP.CLASS) in the WORK library and then the PROC SQL step creates a macro variable containing the names of the files just created based on the LIBNAME='WORK' and MEMNAME contains 'CLASS' (your criteria may be different for choosing). Finally, the macro variable created in the PROC SQL step is used in a SET statement.
** 1) Make Some Data (you do not need this step);
fileind = 1;
fileind = 2;
fileind = 3;
** 2) Discover names of all "CLASS" files in Work;
** LIBNAME AND MEMNAME values should be uppercased.;
** Your selection criteria (WHERE clause) may be different.;
into :alldata separated by ' '
where libname = 'WORK' and
memname contains 'CLASS';
%put The names of the files to concatenate are: &alldata;
** 3) Use the macro variable created above in a SET statement ;
** and verify that the data was created correctly;
Just one more thing: Why duplicate data if there are views?
And if there are more SAS tables in the future instead of processing everything "physically" you can just rebuild the view.
/*libname MyLib 'path to your directory';*/
libname MyLib (sasuser);
proc sql noprint;
select catx('.',libname,memname) into :TblList separated by ' '
where libname='MYLIB' and memtype='DATA'
%put The following tables are in the library:;
data work.MyView / view=work.MyView;