SAS tip: Use the hyphen and colon operators to specify multiple data sets on the SET statement

Here's a SAS tip for you. Most SAS programmers know that SAS provides syntax that makes it easy to specify a list of variables. For example, you can use the hyphen and colon operators to specify lists of variables on many SAS statements:

You can use the hyphen operator (-) to specify a range of variables that have a common prefix and a sequential set of numerical suffixes. For example, the syntax X1-X4 specifies the variables X1, X2, X3, and X4.

You can use the colon operator (:) to specify a list of variables that begin with a common prefix. The colon is a wildcard operator that matches any characters that follow the prefix. For example, the syntax X: specifies any variables that begin with X. That might include variables such as X, X1, XRAY, and XYLEM.

Fewer programmers know that that these operators can also be used on the SET statement to specify a list of names for data sets. That is, the SET statement in the DATA step supports the hyphen and colon operator for specifying a list of data set names.

A review of variable lists

I have previously written about how to specify a list of variables in SAS, including how to use the hyphen and colon operators. The following DATA step uses the hyphen operator to create four variables X1, X2, X3, and X4, as well as two other variables:

/* Use a single hyphen (-) to specify a range of variables that have a common prefix and a sequential set of numerical suffixes. */
data Dat1;
input x1-x4 c $ xray; /* x1-x4 is shorthand for INPUT x1 x2 x3 x4; */
datalines;
1 2 3 4 A 1.2
5 6 7 8 B 2.3
9 10 11 12 C 3.4
;

proc print; run;

By the way, did you know that you can also use the hyphen operator to specify variable names in descending order based on the value of the numerical suffix? For example, modify the INPUT statement and rerun the previous program as follows:

input x4-x1 c $ xray; /* you can also read variable in descending order X4, X3, X2, X1 */

The hyphen operator in this example generates the list of names X4, X3, X2, and X1. Pretty cool, right?

You can use the color operator to specify a list of variables that begin with a common prefix. This is only useful when you know the names of all variables in the data set, and you are sure that you want to read all that begin with the same prefix. In the following example, using KEEP X: will keep not only X1-X4, but also the variable named XRAY. This might or might not be what you want to happen. If you don't want to keep XRAY, use a different syntax such as KEEP X1-X4.

/* Use the colon operator (:) to specify a list of variables that begin with a common prefix. */
data Dat3;
set Dat2;
keep x:; /* Note: Includes ANY variable that starts with 'X' */
run;

proc print; run;

The hyphen operator for specifying data sets on the SET statement

Not many programmers know that the SET statement also supports the hyphen and colon operators. The following example creates four data sets that have the same variables. You can use the syntax SET A1-A4 to concatenate the data sets:

/* create four data sets, A1-A4 */
data A1; x1 = 1; x2 = 11; c = 'A'; run;
data A2; x1 = 2; x2 = 12; c = 'B'; run;
data A3; x1 = 3; x2 = 13; c = 'C'; run;
data A4; x1 = 4; x2 = 14; c = 'D'; run;

data Dat4;
set A1-A4; /* read data sets A1, A2, A3, and A4 on the SET statement */
run;

proc print; run;

This trick can be a good way to concatenate many small data sets into one large one. It is often easier for SAS to work with one large data set. For example, you can use BY-group processing on the large data to replace calling a procedure repeatedly on the small data sets.

As mentioned previously for variable names, you can use the hyphen operator to list the data sets in descending order: set A4-A1.

The colon operator for specifying data sets on the SET statement

The following example uses the colon operator to specify all data sets in the WORK library that begin with 'A':

data Dat6;
set A:; /* all data sets that start with 'A' */
run;

proc print; run;

Again, be careful when using the colon operator! It concatenates all data sets whose names begin with the specified prefix! You might accidentally concatenate data sets that are not part of your analysis.

Summary

Most SAS programmers know about the hyphen operator and the colon operator for specifying a list of variable names that start with the same prefix. This article shows that you can use the same syntax on the SET statement in the DATA step to specify a list of data sets whose names start with the same prefix.