Working with SAS Data Sets


Use the CREATE Statement with the FROM Option

You can create a SAS data set from a matrix by using the CREATE statement with the FROM option. This form of the CREATE statement is as follows:

  • CREATE SAS-data-set FROM matrix-name <[COLNAME=column-name
    ROWNAME=row-name]>
    ;

where

SAS-data-set

specifies the name of the new data set.

matrix

specifies the matrix that contains the data.

column-name

specifies names for the data set variables.

row-name

adds a character variable that identifies each row in the data set.

Suppose you want to create a SAS data set to contain a variable with the height-to-weight ratio for each student. The following statements read variables from the Sashelp.Class data set, form the ratio, and use the CREATE and APPEND statements to write a new SAS data set called Ratio:

proc iml;
use Sashelp.Class;
read all var {Name Height Weight};

HWRatio = Height/Weight;
create Ratio from HWRatio[colname="HtWt"];
append from HWRatio;
show contents;
close Ratio;

Figure 7.18: New Data Set from a Matrix

DATASET : WORK.RATIO.DATA                                                       
                                                                                
 VARIABLE                          TYPE  SIZE                                   
 --------------------------------  ----  ----                                   
 HtWt                              num      8                                   
                                                                                
Number of Variables   : 1                                                       
Number of Observations: 19                                                      
                                                                                



The variable in the Ratio data set is called HtWt. If you do not specify the COLNAME= option, the variables in the new data set are named COL1, COL2, and so forth.