Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 08-21-2017 08:07 AM
(1643 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Regards

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

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.