Set multiple datasets with similar names in sas

14,242

Solution 1

Use a variable name wildcard :

data bigdataset;
 set name_:;
run;

A colon following a variable name prefix selects any variable whose name starts with that prefix. This ability of the colon along with some simple naming standards enables the programmer to manage temporary variables better, format many variables quicker, determine unknown number of variables, clean up macro generated datasets, and shorten the code for variety of PROCS. For example

data ADLB;
set  lb:;

This DATA step reads all the data sets in the WORK library that begin with LB. Also, when the programmer coded this step, he/she did not need to know how many dataset are read, only that he/she wants to read all of the dataset with a particular prefix. Both colon and dash lists also work with the MERGE statement.

Quoted from Using SAS Colon Effectively

Solution 2

This will work whether or not the data sets have a common prefix. Of course for your situation the COLON modifier is a really good solution.

PROC SQL noprint;
    SELECT CATS(libname,".",memname) into :DSNS separated by " "
    FROM DICTIONARY.TABLES 
    WHERE UPCASE(LIBNAME)="YOUR_LIBNAME";
QUIT;
%Put DATA SETS: &DSNS;

Data BIGDATASET;
    Set &dsns;
Run;
Share:
14,242
Lyle
Author by

Lyle

Code in SAS, Python, and UNIX servers for applied mathematics work in Market Research as a Data Scientist.

Updated on June 25, 2022

Comments

  • Lyle
    Lyle almost 2 years

    Suppose I have a varying number of datasets in my work environment, but all of which start with a similar name: name_abc, name_efg, name_1ky, etc. The datasets have the same variables and characteristics, and I want to set them all into one dataset.

    data bigdataset;
        set [all datasets that begin with name_];
    run;
    

    Is there a way I can do this in SAS without typing all the datasets? I need this to be flexible with the number of datasets available in work environment.

  • Joe
    Joe about 9 years
    Umm, those edits entirely missed Reeza's initial point. prefix: was correct. prefix = the prefix common to the datasets...
  • Joe
    Joe about 9 years
    @skobaljic, if you added that quote from a source like the documentation, please add the reference to that source as well.
  • skobaljic
    skobaljic about 9 years
    Added, hope it is useful to someone.