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

Hey to all,

I would like to know how to delete in array and matrix using sas proc fcmp. And also, I wanted to know if can call the same subroutine

such I did at line 49.

Here is the code I made.

Regards

 

proc fcmp outlib=work.func.matrix;
  subroutine GSPfunc(mat[*,*],GSP[*,*]);
    outargs GSP;

epsilon= 10 ** -10;
array vectors{1,1} _temporary_;
array t_vectors{1,1} _temporary_;
call dynamic_array(vectors, dim(mat,1),dim(mat,1));
call dynamic_array(t_vectors, dim(mat,1),dim(mat,1));

array values{dim(mat,1)} _temporary_;
array mat2{1,1} _temporary_;
call dynamic_array(mat2, dim(mat,1),dim(mat,1));

/*Calculation of Eigenvecteurs and Eigenvalues by suroutine Jacobi*/
call jacobi(mat,values,vectors,nrot);

do i= dim to 1 by -1;
	if values[i]<= epsilon then  do;
		if  values[i]< -epsilon then IsNegative= 1 do; 
			delete /* value[i] ?*/;
delete /*vectors[i,.] ?*/; /*HELP ME HERE: I want to delete the value[i] and the corresponding vectors[i,.]*/ end;
end; else do; do j= dim to 1 by -1; vectors[j,i]= vectors[j,i] * sqrt(values[i]); end; end; end; if IsNegative= 1 then do; /*In this case, I'm bulding a new matrix: mat2, that is set with mat without vectors[i,.]*/ call transpose(vectors, t_vectors); call mult(vectors, t_vectors, mat2); do i= 1 to dim(mat2,1); mat2[i, i] = Sqrt(mat2[i, i]); end; do i= 1 to dim(mat2,1)-1; do j= i+1 to dim(mat2,1); mat2[i,j]= mat2[i,j] / (mat2[j,j] * mat2[i, i]); mat2[j,i]= mat2[i,j]; end; end; do i= 1 to dim(mat2,1); mat2[i,j]= 1; end; /*repete all the process on mat2*/ do i= 1 to dim(vectors,1); do j= 1 to dim(vectors,2); GSP[i,j] = GSPfunc(mat2,GSP]; end; end; end; else if IsNegative ne 1 then do; do i= 1 to dim(vectors,1); do j= 1 to dim(vectors,2); GSP[i,j] = vectors[i,j]; end; end; end; endsub; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

1. What are you trying to accomplish?

2. Are your input matrices always symmetric?

3. Do you have SAS/IML software?

 

It looks like maybe you are trying to detect whether a matrix is positive definite by checking if it has negative eigenvalues. It looks like you want to delete the eigenvectors that correspond to the negative eigenvalues.  Am I close?

 

 

 

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

1. What are you trying to accomplish?

2. Are your input matrices always symmetric?

3. Do you have SAS/IML software?

 

It looks like maybe you are trying to detect whether a matrix is positive definite by checking if it has negative eigenvalues. It looks like you want to delete the eigenvectors that correspond to the negative eigenvalues.  Am I close?

 

 

 

DoumbiaS
Quartz | Level 8

 

 

Trying to answer to you, I accidentally click on "accep as solution". Could you re-edit my post please ?

 

I don't have proc iml.

 

Yes absolutly, my input matrix is alway symmetric. It's a symetric correlation matrix and I calculate its

EigenVectors and EigenValues.

 

When one EigenValue is negative, I delete it and the corresponding EigenVectors. And  I re-bult a new symetric correlation matrix, that is mat2.

 

Regards

Rick_SAS
SAS Super FREQ

I don't have the authority to "un-accept" the solution. 

 

If the input matrices are correlation matrices, then they are always positive semidefinite. That is, they will never have negative eigenvalues. There might be one or more zero eigenvalues if the correlation matrix is rank-deficient.

 

If you don't have PROC IML, then the next best solution is to use PROC PRINCOMP to compute the eigenvectors and eigenvalues. Because the eigenvectors are ordered by the magnitude of the eigenvalues, you can use the "Eigenvalues" table to figure out how many eigenvalues/eigenvectors you want to keep.

 

The part of the code that you are asking about is for handling the eigenvectors that are associated with negative eigenvalues. As I've said, negative eigenvalues will never occur for a valid correlation matrix. If you are getting an invalid correlation matrix from PROC CORR, it might be because you are not using the NOMISS option, as explained in the article "When is a correlation matrix not a correlation matrix."  Add the NOMISS option and the correlation matrix will always be positive semidefinite.

 

 

DoumbiaS
Quartz | Level 8

I' ll try with proc corr and this option and check. Thank you.

 

Regards

DoumbiaS
Quartz | Level 8

Using proc corr nomiss option and after that proc princomp gives different eigenvalues or eigenvectors in the case where

one would get a non semi defite positive correl matrix with nomiss.

Rick_SAS
SAS Super FREQ

Different from what?  If you'd like assistance, please post the code you are running, the results you are seeing, and specify what part of the results seem unusual to you.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 2615 views
  • 0 likes
  • 2 in conversation