Working with SAS Data Sets


Syntax for Specifying a SAS Data Set

You can specify a SAS data set by using a literal value, such as "Sashelp.Class," or by specifying an expression that resolves to the name of a SAS data set. Most of the examples in this chapter use a literal value, such as the following statements:

proc iml;
use Sashelp.Class;
read all var _NUM_ into X;
close Sashelp.Class;

The statements open the Sashelp.Class data set, read all the numerical variables into a matrix named X, and close the Sashelp.Class data set. The previous statements are equivalent to the following statements, which use an expression (which must be enclosed in parentheses) to specify the data set:

dsname = "Sashelp.Class";
use (dsname);
read all var _NUM_ into X;
close (dsname);

This alternate syntax is available for specifying a data set name in the CLOSE, CREATE, EDIT, SETIN, SETOUT, SORT, and USE statements.

You can use expressions to interact with a data set whose name is not known until run time. For example, the following statements read several data sets and perform an analysis on each:

lib = "Sashelp";
dsnames = {"Class" "Enso" "Iris"};
do i = 1 to ncol(dsnames);
   dsname = concat(lib,".",dsnames[i]);
   use (dsname); /* Sashelp.Class, Sashelp.Enso, etc. */
   read all var _NUM_ into X[c=varNames];
   /* do something with the data in X */
   print dsname varNames;
   close (dsname);
end;

Figure 7.1: Looping over Data Sets

dsname varNames  
Sashelp.Class Age Height Weight

dsname varNames  
Sashelp.Enso Month Year Pressure

dsname varNames  
Sashelp.Iris SepalLength SepalWidth PetalLength PetalWidth