I'm want to read all the SAS datasets in a directory into a common datasets. The individual datasets will expand over time. Using the SAS libname, is there a way to do it without having to itemize each dataset name in the SET statement?
Do you want to try the code below? replace'your_libname' with your real libname and using capital letters in "
where libname='YOUR_LIBNAME'"
libname your_libname '???';
proc sql noprint;;
select catx('.','your_libname',memname) into : dsn separated by ' '
from dictionary.tables
where libname='YOUR_LIBNAME'; /* libname must be capital letters */
quit;
%put &dsn;
data want;
set &dsn;
run;
Linlin
Do you have control over the filenames? I ask because, if they can all start with the same character or string, then you can use a wildcard in your set statement. e.g.:
libname mydata "c:\art\test";
data mydata.file1;
set sashelp.class;
run;
data mydata.file2;
set sashelp.class;
run;
data want;
set mydata.f:;
run;
I don't currently, but the current number of datasets is relatively small, so that's an easy change fo fix the problem. Thanks!!
As your task becomes more complicated there are some macro language solutions as well. Until then stick with the simple.
Do you want to try the code below? replace'your_libname' with your real libname and using capital letters in "
where libname='YOUR_LIBNAME'"
libname your_libname '???';
proc sql noprint;;
select catx('.','your_libname',memname) into : dsn separated by ' '
from dictionary.tables
where libname='YOUR_LIBNAME'; /* libname must be capital letters */
quit;
%put &dsn;
data want;
set &dsn;
run;
Linlin
Thanks, that worked beautifully and I didn't have to change any dataset names. I have multiple types of datasets in the directory and was able to select the ones I wanted by adding :and memname like '%diff%' to the where clause.
proc sql
As mentioned there is the relatevely new name range syntax for the SET statement data set names argument. You will also want to look in the documentation for the INDSNAME=variable option. And if the datasets have the same variables and lengths you can gain performance with OPEN=DEFER.
So the data are not static ("will expand over time") and will live in two places (the individual data sets and the common one). That's parallel maintenance, something to be avoided. Consider making the common data set a view.
Mike7085 wrote:
I'm want to read all the SAS datasets in a directory into a common datasets. The individual datasets will expand over time. Using the SAS libname, is there a way to do it without having to itemize each dataset name in the SET statement?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.