Hi all,
im using the following code for obtaining eigenvalues / vectors for deriving principal components .
My data-lines have a covariance matrix.
data have(type=cov); /* leave off a lot of the TYPE=COV data for simplicity */
_type_='COV';
input m1 m2 m3 m4;
datalines;
18.8767 26.8567 7.5550 -5.1533
26.8567 47.2433 5.1033 -15.2300
7.5550 5.1033 92.1900 58.8983
-5.1533 -15.2300 58.8983 48.5233
;
ods select eigenvalues eigenvectors;
proc princomp data=have(TYPE=COV) cov;
run;
proc iml;
use have; read all var _NUM_ into M; close;
eigm=eigval(m);
print eigm;
It works perfectly but what do i need to do while trying to use a correlation matrix ? when i change the cov to cor i get an error that the system is not expecting COR
So I tried the code below but this throws an error
ERROR: (execution) Matrix should be square.
ERROR: Matrix eigvals has not been set to a value.
proc iml;
sigma={1.00000 0.89933 0.18110 -0.17027
0.89933 1.00000 0.07733 -0.31809
0.18110 0.07733 1.00000 0.88061
-0.17027 -0.31809 0.88061 1.00000};
eigvals=eigval(sigma);
eigvec=eigvec(sigma);
print eigvals;
print eigvec;
Many thanks.
I think the issue with PRINCOMP is that the correct syntax is TYPE=CORR with two 'R's.
The error message from IML is informative since sigma is not square, actually it is a row vector. To declare a matrix you need to add commas at the end of the rows like this:
sigma={ 1.00000 0.89933 0.18110 -0.17027,
0.89933 1.00000 0.07733 -0.31809,
0.18110 0.07733 1.00000 0.88061,
-0.17027 -0.31809 0.88061 1.00000};
I think the issue with PRINCOMP is that the correct syntax is TYPE=CORR with two 'R's.
The error message from IML is informative since sigma is not square, actually it is a row vector. To declare a matrix you need to add commas at the end of the rows like this:
sigma={ 1.00000 0.89933 0.18110 -0.17027,
0.89933 1.00000 0.07733 -0.31809,
0.18110 0.07733 1.00000 0.88061,
-0.17027 -0.31809 0.88061 1.00000};
Thank you!!
You do not need to type in the matrix. You can use the COV2CORR function in SAS/IML to rescale the covariance matrix into a correlation matrix.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.