I'm converting SAS code into Python and I've noticed a difference between the results of a SAS procedure and its Python equivalent.
For example :
The SAS code :
proc iml;
abc = {1 2 3 1,
4 5 6 1,
7 8 9 1,
1 1 1 1};
/* Calculation of eigenvalues (eigm) and eigenvectors (eigv) */
eigm = eigval(abc);
eigv = eigvec(abc);
print eigm, eigv;
quit;
the result :
The Python equivalent of this SAS code :
import numpy as np
# Definition of matrix abc
abc = np.array([[1, 2, 3, 1],
[4, 5, 6, 1],
[7, 8, 9, 1],
[1, 1, 1, 1]])
# Calculation of eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(abc)
print("Eigenvalues :", eigenvalues)
print("Eigenvectors :\n", eigenvectors)
Remarks:
It's easy to see that the values are the same in both sets of results, but they don't follow the same layout.
If layout is an issue then send one or more sets of results to a data set and then write code to show in the desired layout (and rounded/formatted for similarity if desired).
So it's just an issue of the ordering of the eigenvector columns, right? The SAS docs explain the SAS ordering:
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.3/imlug/imlug_langref_sect123.htm
The numpy docs explain that eigenvalues (and thus eigenvectors) are "not necessarily ordered."
https://numpy.org/doc/stable/reference/generated/numpy.linalg.eig.html
I've moved this post to the IML board, which more closely aligns with this question. As pointed out by a previous answer, from the IML documentation, the eigenvalues for a non-symmetric matrix are sorted first by their real parts, then by the magnitude of their imaginary parts. You can get sorted eigenvalues from numpy using the numpy.linalg.eigh() function, but note that that function sorts eigenvalues in ascending order.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.