Language Reference

CREATE Statement

  • CREATE SAS-data-set <VAR operand>;

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

This statement is supported only by the IML procedure.

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 matrices. 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 matrices 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-names

specifies one or more matrices that contain 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 after 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 sum of the number of columns in each 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.

The following modifies the previous example to show how to create a data set from two or more matrices. The data are in the numerical matrix x and the character matrix y.

x = {1 2 3, 4 5 6};
y = {A B, C D};
varNames = ("X1":"X3") || ("Y1":"Y2") ;
rowNames = {"row1" "row2"};
/* create data set MYDATA with variables rowNames, X1, X2, X3, Y1, Y2 */
create MyData from x y [colname = varNames rowname = rowNames];
append from x y [rowname = rowNames];
close MyData;

Writing Data That Contain 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 90: Data Set That Contains Formats

ObsDateTime
1March 20, 20105:32:00 PM
2March 20, 201111:21:00 PM
3March 20, 20125:14:00 AM
4March 20, 201311:02:00 AM
5March 20, 20144:57:00 PM
6March 20, 201510:45:00 PM


Last updated: May 07, 2026