DATA Step, Macro, Functions and more

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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?


Accepted Solutions
Solution
‎03-30-2012 01:12 PM
Super Contributor
Posts: 1,636

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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

View solution in original post


All Replies
PROC Star
Posts: 7,366

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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;

Occasional Contributor
Posts: 10

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

I don't currently, but the current number of datasets is relatively small, so that's an easy change fo fix the problem.  Thanks!!

Valued Guide
Posts: 632

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

As your task becomes more complicated there are some macro language solutions as well.  Until then stick with the simple.

Solution
‎03-30-2012 01:12 PM
Super Contributor
Posts: 1,636

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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

Occasional Contributor
Posts: 10

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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

Respected Advisor
Posts: 3,777

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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.

Regular Contributor
Posts: 184

Is there a way to read all SAS datasets in a directory into one dataset without specifying them in individual set statements

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?

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1125 views
  • 3 likes
  • 6 in conversation