The GVARCLUS Procedure
Example 12.2 Output a Covariance Matrix to a SAS Data File
This example shows how to output a covariance matrix to a SAS data file. The OUTCP= option creates an output data table named mycas.cov.
The following DATA step generates a data table that has 2,000 observations and contains both interval variables (x1–x2) and nominal variables (a, c1, and c2):
data mycas.data1;
array x{2};
array c{2};
do i=1 to 2000;
a=int(rand('UNIFORM')*2);
do j=1 to 2;
x{j}=rand('UNIFORM');
c{j}=int(rand('UNIFORM')*2);
end;
output;
end;
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements invoke the GVARCLUS procedure:
title "Output the Covariance Matrix";
proc gvarclus data=mycas.data1 maxiter=4 outcp=mycas.cov;
input c1 c2 /level=nominal;
input x1 x2 /level=interval;
run;
proc print data=mycas.cov;
run;
Output 12.2.1 shows the content of the data file that PROC GVARCLUS generates.
Output 12.2.1: Output the Covariance Matrix
| Output the Covariance Matrix |
| Obs | _ID_ | _TYPE_ | _VAR_ | _vID_ | v1 | v2 | v3 | v4 | v5 | v6 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | MEAN/FREQ | 0.49 | 0.50 | 1001.00 | 999.00 | 967.00 | 1033.00 | ||
| 2 | 2 | N | 2000.00 | 2000.00 | 2000.00 | 2000.00 | 2000.00 | 2000.00 | ||
| 3 | 3 | COV | x1 | v1 | 157.43 | -3.35 | 4.74 | -4.74 | -4.61 | 4.61 |
| 4 | 4 | COV | x2 | v2 | -3.35 | 168.47 | -0.08 | 0.08 | 6.02 | -6.02 |
| 5 | 5 | COV | c1 0 | v3 | 4.74 | -0.08 | 500.00 | -500.00 | 1.02 | -1.02 |
| 6 | 6 | COV | c1 1 | v4 | -4.74 | 0.08 | -500.00 | 500.00 | -1.02 | 1.02 |
| 7 | 7 | COV | c2 0 | v5 | -4.61 | 6.02 | 1.02 | -1.02 | 499.46 | -499.46 |
| 8 | 8 | COV | c2 1 | v6 | 4.61 | -6.02 | -1.02 | 1.02 | -499.46 | 499.46 |
The _VAR_ column displays the names of all variables and the levels of the nominal variables. Assuming that you have n effects (the total number of interval variables and the levels of categorical variables), the _vID_ column contains n markers, v1 to vn, where vi denotes the ith effect. The column _TYPE_ defines the role of each row. When the _TYPE_ column displays MEAN/FREQ, the corresponding row contains either the mean for an interval variable or the frequency for a level of a nominal variable. When the _TYPE_ column displays N, the corresponding row contains the number of samples. And when the _TYPE_ column displays COV, the corresponding row contains a row of the covariance matrix. In this example, the covariance matrix is , and it resides in the table in rows 3–6 and columns 7–10.