CREATE Statement

CREATE SAS-data-set <VAR operand> ;

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

The CREATE statement creates a new SAS data set and makes it both the current input and output data sets. The variables in the new SAS data set are either the variables listed in the VAR clause or variables created from the columns of the FROM matrix. The FROM clause and the VAR clause should not be specified together.

When you write to a SAS data set, the variable types and lengths correspond to the attributes of the vectors specified in the VAR clause or the matrix in the FROM clause.

To add observations to your data set, you must use the APPEND statement.

The arguments to the CREATE statement are as follows:

SAS-data-set

is the name of a SAS data set. It can be specified with a one-level name (for example, A) or a two-level name (for example, Sasuser.A). You can also specify an expression (enclosed in parentheses) that resolves to the name of a SAS data set. See the example for the CLOSE statement.

operand

specifies a set of existing SAS/IML matrices that contain data. The names of the matrices become the names of the data set variables. If you do not specify the name of a variable, all variables in scope are assumed. You can specify variables by using any of the methods described in the section Select Variables with the VAR Clause.

matrix-name

specifies a matrix that contains the data. Each column of the matrix produces a variable in the data set.

column-name

is a character matrix or quoted literal that contains names of the data set variables.

row-name

is a character matrix or quoted literal that contains text to associate with each observation in the data set.

Writing Data from Vectors

The following example demonstrates ways that you can use the VAR clause:

x1 = T(1:5);
x2 = T(5:1);
y = {-1,0,1,0,1};
z = {a,b,c,d,e};
create temp var {x1 y z};    /* a literal matrix of names      */
append;
close temp;

varNames = {"x1" "y" "z"}; 
create temp var varNames;    /* a matrix that contains names  */
append;
close temp;
free varNames;

create temp var ("x1":"x2"); /* an expression                  */
append;
close temp;

create temp var _all_;       /* all variables in scope         */
append;
close temp;

For a more realistic example, the following statements create a new SAS data named Population that contains two numeric and two character variables:

State   = {"NC",      "NC",   "FL",     "FL"};
County  = {"Chatham", "Wake", "Orange", "Seminole"};
Pop2000 = {49329,     627846,  896344,  365196};
Pop2009 = {64772,     897214, 1086480,  413204};
create Population var {"State" "County" "Pop2000" "Pop2009"};
append; 
close Population;

The data come from vectors with the same names. You must initialize the character variables (State and County) prior to calling the CREATE statement. The State variable has length 2 and the County variable has length 8. The Pop2000 and Pop2009 variables are numeric.

Writing Data from a Matrix

The following example uses the FROM clause with the COLNAME= option to create a SAS data set named MyData. The new data set has variables named with the COLNAME= operand. The data are in the FROM matrix x, and there are two observations because x has two rows of data. The COLNAME= operand gives descriptive names to the data set variables, as shown in the following statements:

x = {1 2 3, 4 5 6};
varNames = "x1":"x3";
/* create data set MYDATA with variables X1, X2, X3 */
create MyData from x [colname=varNames];
append from x;
close MyData;

As shown in the example, you can specify a COLNAME= and a ROWNAME= matrix in the FROM clause. The COLNAME= matrix gives names to variables in the SAS data set being created. The COLNAME= operand specifies the name of a character matrix. The first ncol values from this matrix provide the variable names in the data set being created, where ncol is the number of columns in the FROM matrix. The CREATE statement uses the first ncol elements of the COLNAME= matrix in row-major order.

The ROWNAME= operand adds a variable to the data set that contains labels. The operand must be a character matrix. The length of the resulting data set variable is the length of a matrix element of the operand. The same ROWNAME= matrix should be used in any subsequent APPEND statements for this data set.

Writing Data That Contains Formats

If you associate a format with a matrix by using the MATTRIB statement, then the CREATE statement assigns that format to the corresponding variable in the data set, as shown in the following example:

proc iml;
date = { '20MAR2010'd, '20MAR2011'd, '20MAR2012'd, 
         '20MAR2013'd, '20MAR2014'd, '20MAR2015'd };
mattrib date format=WORDDATE.;

/* time of equinox, GMT (Greenwich Mean Time) */
time = { '17:32't,     '23:21't,     '05:14't,  
         '11:02't,     '16:57't,     '22:45't };
mattrib time format=TIMEAMPM.;

create MarchEquinox var {"Date" "Time"};
append;
close MarchEquinox;

proc print data=MarchEquinox; 
run;

Figure 23.76: Data Set That Contains Formats

Obs Date Time
1 March 20, 2010 5:32:00 PM
2 March 20, 2011 11:21:00 PM
3 March 20, 2012 5:14:00 AM
4 March 20, 2013 11:02:00 AM
5 March 20, 2014 4:57:00 PM
6 March 20, 2015 10:45:00 PM