READ Statement

READ <range> <VAR operand> <WHERE(expression)> <INTO name <[ROWNAME=row-name COLNAME=column-name]>> ;

The READ statement reads observations from the current SAS data set. For example, the following statements read data from the Sashelp.Class data set:

use Sashelp.Class;
read all var {Sex Height}; /* creates vectors Sex and Height */
read all var _NUM_ into X[colname=varNames]; /* numerical data */
read all var {Weight} where(Sex='M'); /* vector of male weights */
read point 10 var {Name}; /* 10th name in data set */
close Sashelp.Class;

See Chapter 7 for further examples.

The arguments to the READ statement are as follows:

range

specifies a range of observations. If range is not specified, the current observation is read. You can specify a range of observations by using the ALL, CURRENT, NEXT, AFTER, and POINT keywords, as described in the section Process a Range of Observations.

operand

selects a set of variables. If the VAR clause is omitted, all variables are read into vectors whose names are identical to the names of the variables in the data set. As described in the section Select Variables with the VAR Clause, you can specify variable names by using a matrix literal, a character matrix, an expression, or the _ALL_, _CHAR_, or _NUM_ keywords.

expression

specifies a criterion by which certain observations are selected. If the WHERE clause is omitted, no subsetting occurs. The optional WHERE clause conditionally selects observations that are contained within the range specification. For details about the WHERE clause, see the section Process Data by Using the WHERE Clause.

name

is the name of the target matrix.

row-name

is a character matrix or quoted literal that contains descriptive row labels.

column-name

is a character matrix or quoted literal that contains descriptive column labels.

The range, VAR, WHERE, and INTO clauses are all optional and can be specified in any order.

Use the READ statement to read variables or records from the current SAS data set into column matrices of the VAR clause or into the single matrix of the INTO clause. When the INTO clause is used, each variable in the VAR clause becomes a column of the target matrix, and all variables in the VAR clause must be of the same type. If you specify no VAR clause, the default variables for the INTO clause are all numeric variables. Read all character variables into a target matrix by using VAR _CHAR_.

Reading Variables into Columns of a Matrix

When you use the INTO clause, the specified variables are read into the columns of a matrix.

You can specify ROWNAME= and COLNAME= matrices as part of the INTO clause. The COLNAME= matrix specifies the name of a new character matrix to be created. This COLNAME= matrix is created in addition to the target matrix of the INTO clause and contains variable names from the input data set corresponding to columns of the target matrix. The COLNAME= matrix has dimension $1 \times $nvar, where nvar is the number of variables contributing to the target matrix.

The ROWNAME= option specifies the name of a single character variable in the input data set. The values of this variable are put in a character matrix with the same name as the variable. This matrix has the dimension nobs$\times 1$, where nobs is the number of observations in the range of the READ statement.

Row and column names created via a READ statement are permanently associated with the INTO matrix. You do not need to use a MATTRIB statement to get this association.