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.

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).

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.

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.

You can specify a set of variables to use with the VAR clause. The operand argument can be specified in one of the following ways:

  • a literal that contains variable names

  • the name of a matrix that contains variable names

  • an expression in parentheses that yields variable names

  • one of the keywords in the following list:

    _ALL_

    specifies that all variables in scope should be written to the data set.

    _CHAR_

    specifies that all character variables in scope should be written to the data set.

    _NUM_

    specifies that all numeric variables in scope should be written to the data set.

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;

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.

The variable types and lengths correspond to the attributes of the matrices specified in the VAR clause or the matrix in the FROM clause. The default type is numeric when the name is undefined. If you do not specify the name of a variable, then all variables in scope are used.

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

For 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.

In the next example, you use 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;

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.75 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