BookmarkSubscribeRSS Feed
RO_C
Calcite | Level 5

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 :

image (1).png

 

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)

 

unnamed1.png

 

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.

5 REPLIES 5
ballardw
Super User

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).

RO_C
Calcite | Level 5
In fact, the matrix I have is much larger than this one. I don't know what movements there will be in the other columns
Quentin
Super User

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

 

BASUG is hosting free webinars Next up: Mark Keintz presenting History Carried Forward, Future Carried Back: Mixing Time Series of Differing Frequencies on May 8. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
RO_C
Calcite | Level 5
I need to use these results in another step, in which case the results obtained in SAS will be different from those obtained in Python, except that I absolutely must find the same results in SAS as in Python.
Mike_N
SAS Employee

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. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 5 replies
  • 622 views
  • 6 likes
  • 4 in conversation