BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
axelpuri
Fluorite | Level 6

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. 

 
1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

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};

View solution in original post

3 REPLIES 3
IanWakeling
Barite | Level 11

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};
axelpuri
Fluorite | Level 6

Thank you!!

Rick_SAS
SAS Super FREQ

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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1406 views
  • 8 likes
  • 3 in conversation