APPEND Statement |
The APPEND statement adds observations to the end of a SAS data set.
The arguments to the APPEND statement are as follows:
is specified as one of the following:
a literal matrix that contains variable names
a character matrix that contains variable names
an expression in parentheses that yields variable names
one of the following keywords:
for all variables
for all character variables
for all numeric variables
is the name of a matrix that contains data to append.
is a character matrix or quoted literal that contains descriptive row names.
You can use the APPEND statement to add data to the end of the current output data set. The appended observations are from either the variables specified in the VAR clause or variables created from the columns of matrix. The FROM clause and the VAR clause cannot be specified together.
The APPEND statement is usually used without any arguments. A common practice is to specify the data in the CREATE statement, as shown in the following example:
proc iml; x = {1,2,3,4}; /* 4 x 1 vector */ y = {4 3,2 1}; /* 2 x 2 matrix */ z = {2,3,4}; /* 3 x 1 vector */ c = {A,B,C,D}; /* 4 x 1 character vector */ create Temp1 var {x y}; /* Temp1 contains two variables */ append; /* appends data from x and y */ close Temp1; quit; proc print data=Temp1 noobs; run;
The values in the Temp1 data set are shown in Figure 23.39. Notice that the matrix y is written to the data set in row-major order.
X | Y |
---|---|
1 | 4 |
2 | 3 |
3 | 2 |
4 | 1 |
If you omit the VAR (and FROM) clause in the CREATE statement, then the new data set contains a variable for each SAS/IML matrix that is in scope. You can use the VAR clause in the APPEND statement to write specific variables. Variables that are not explicitly specified receive missing values, as shown in the following statements:
proc iml; x = {1,2,3,4}; /* 4 x 1 vector */ y = {4 3,2 1}; /* 2 x 2 matrix */ z = {2,3,4}; /* 3 x 1 vector */ c = {A,B,C,D}; /* 4 x 1 character vector */ create Temp2; /* Temp2 contains a variable for each matrix */ append var {c x z}; /* y gets missing values */ close Temp2; quit; proc print data=Temp2 noobs; run;
The values in the Temp2 data set are shown in Figure 23.40. The data set contains four observations because that is the number of elements in the matrix with the greatest number of elements. Elements are appended in row-major order. Notice that the variable z contains a missing value at the end because the variable was created from a SAS/IML matrix that contained fewer than four elements.
c | x | y | z |
---|---|---|---|
A | 1 | . | 2 |
B | 2 | . | 3 |
C | 3 | . | 4 |
D | 4 | . | . |
As shown in the previous example, the default variables for the APPEND statement are all matrices that match variables in the current data set with respect to name and type.
The ROWNAME= option in the FROM clause specifies the name of a character matrix to contain row titles. Use this option in conjunction with the identical option in the FROM clause of the CREATE statement, as shown in the following statements:
proc iml; VarName = {"x" "y"}; w = {3 96, 4 90, 2 100, 4 92}; /* data matrix */ cov = cov(w); /* sample covariance matrix of data */ create Temp3 from cov[rowname=VarName colname=VarName]; append from cov[rowname=VarName]; close Temp3; quit; proc print data=Temp3 noobs; run;
The values in the Temp3 data set are shown in Figure 23.41. The matrix cov contains the data that is saved to the Temp3 data set. The character vector VarName contains the names of the variables for the Temp3 data set. (If you use the FROM clause in the CREATE statement, but do not specify the COLNAME= option, then the variables are named COL1, COL2, and so on.) The ROWNAME= option enables you to specify a single character variable when you are creating a data set from a numerical matrix. This is useful for specifying variable names in a correlation or covariance matrix, but can also be used more generally to specify a row label for each observation.
VarName | x | y |
---|---|---|
x | 0.91667 | -4.1667 |
y | -4.16667 | 19.6667 |
If you do not specify the ROWNAME= option in the CREATE statement, then you do not need to specify the ROWNAME= option in the APPEND statement, as shown in the following example:
create Temp3 from cov[colname=VarName]; append from cov; close Temp3;
You can also use the APPEND statement with the EDIT statement. See the documentation for the EDIT statement for examples.