Skip to content

Instantly share code, notes, and snippets.

@swuecho
Created February 23, 2012 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swuecho/1893474 to your computer and use it in GitHub Desktop.
Save swuecho/1893474 to your computer and use it in GitHub Desktop.
SAS IML
*****************************************************************************************/
SAS/IML software gives you access to a powerful and flexible programming language
*(Interactive Matrix Language) in a dynamic, interactive environment.
*
*The fundamental object of the language is a data matrix.
*You can use SAS/IML software interactively (at the statement level) to see results immediately,
*or you can store statements in a module and execute them later.
*The programming is dynamic because necessary activities such as memory allocation and
*dimensioning of matrices are performed automatically.
/******************************************************************************************;
******************************************************************************************;
* Reading Matrix A;
*********************;
proc IML;
A={1 2 3,4 5 6,7 5 9}; *THE MATRIX A IS 3*3. USE COMMA TO SPECIFY THE COLUMN.
*USING SPACE TO SPERATE THE CLOUMN ELEMENT ;
PRINT A; *PRINT OUT THE MATRIX.;
/****************************************
*TRANSPOSE OF A MATRIX & PRINTING THIS;
****************************************/
A_TRANSPOSE=A`;
PRINT A_TRANSPOSE;
/**********************************************************
*THIS COMPUTES the eigenvalues & EIGENVECTORS of MATRIX A;
**********************************************************/
CALL EIGEN( EVALUE,EVECTOR,A);
PRINT EVALUE; * Prints out a vector of eigenvalues;
PRINT EVECTOR; * Prints out a matrix whose columns are EIGENVECTORS
( NORMALIZED, unit length);
/**********************************************************
*FINDING THE INVERSE OF THE MATRIX A;
**********************************************************/
INV_A=INV(A);
PRINT INV_A; *Prints out the INVERSE*;
C = {1 2 3,4 5 6}; PRINT C; *makes a 2x3 matrix;
C1=C[+,]; PRINT C1; *makes a 1x3 matrix by summing the columns;
C2=C[,+]; PRINT C2; *makes a 2x1 matrix by summing the rows;
F = C[,2]; PRINT F; *makes a matrix out of second column of C;
D = DIAG( C[,2] ); PRINT D; *puts second column of c on diagonal;
CC= VECDIAG(D); PRINT CC; *makes a vector out of the diagonal;
DD = EXP(D); PRINT DD; *exponentiates each entry;
E = C || C; PRINT E; *puts c next to itself;
K = REPEAT(C,3,2); PRINT K; *creates a 3x2 matrix with matrix entry C;
G = 3+3*(C[,2:3]##3); PRINT G; *raises each entry of columns 2 & 3 of C
to the third power then multiples
by 3 and adds 3;
H = C ## C; PRINT H; *raises each entry of C to itself;
J = C # C; PRINT J; *multiplies each entry of C by itself;
RUN;
quit;
/**********************************************************
* ANOTHER WAY OF READING MATRIX;
**********************************************************/
DATA TEST;
INFILE cards;
INPUT v1 v2 v3;
cards;
3 1 2
4 1 2
3 0 0
1 0 7
;
RUN;
/*******************************************************************************
*Read all data into an IML matrix X. FOR THIS USE the statements USE and READ.;
*******************************************************************************/
PROC IML;
USE TEST;
READ all var{v1 v2 v3} into X;
n=nrow(X); *n is the number of observations, i.e. number of rows in X matrix;
one=J(N,1,1); *12 x 1 vector of containing ones: UNIT VECTOR;
df=n-1;
mean=(one`*X)/n; *mean vector contains the means;
xm=x-one*mean; *contains the mean corrected (X-1'*MEAN);
sscpm=xm`*xm;
************************************;
* SAMPLE VARIANCE-COVARIANCE MATRIX;
************************************;
s=sscpm/df;
d=diag(S); * CREATING A DIAGONAL MATRIX FROM THE DIAGONAL
ELEMENTS OF S;
***************************************************
*STANDARDIZED MATRIX: THAT IS, (X-1'*MEAN)/SQRT(D);
*YOU NEED THIS MATRIX FOR FINDING CORRELATION MATRIX
*************************************************** ;
xs=xm*sqrt(inv(d));
***************************************************
*CORRELATION MATRIX;
***************************************************;
r=xs`*xs/(n-1);
***************************************************
*DETERMINANT OF S: GENERALIZED VARIANCE
***************************************************;
gv=det(s);
***************************************************
*PRINTING ALL RESULTS;
***************************************************;
print mean,xm,sscpm,s,xs,r,gv;
print X;
RUN;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment