Set multiple datasets with similar names in sas

Lyle picture Lyle · Apr 9, 2015 · Viewed 13k times · Source

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.

Answer

Reeza picture Reeza · Apr 9, 2015

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