Magnus
Magnus

Reputation: 760

How to construct a covariance matrix using PROC IML in SAS

I am writing an assignment in SAS and I'm trying to create a covariance matrix from my data:

PROC IML;
     USE nydata;
     varNames = {"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"};
     READ ALL INTO X;       *read X1 & X2 to X matrix;
     N = NROW(X);           *N = number of observation: NROW is the number of row;
     ONE = J(N,1,1);        *J(nrow,ncol, value) i.e. 12x1 vector containing ones: J rprecents a function to creat matrix of a given dimention;
     A=ONE`;                *vector transpose, it can be also writen as T(ONE) ;
     C= A*X;                *will give 1x2 matrix, wtih a summed value of each columens;
     MEAN = C/N;            *matric containg means;
     MEANS = ONE*MEAN;
     XM= X-MEANS;           *Corrected mean matrix;
     SSCPM=XM`*XM;          *sume of squares and cross products matrix;
     DF=N-1;                *degree of freedom;
     S=SSCPM/DF;            *Covariance matrix;
     D=DIAG(S);             *taking only the individual variances;
     XS=XM*SQRT(INV(D));    *XS,standardized data,SQRT stands of squear root and INV, standes to inverted matrix;
     R=t(XS)*XS/(N-1);      *computing the coorelation matrix;
     GV=DET(S);              * computing from the determinant of the covariance matrix;


     create kovarians from S[colname={"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"}
rowname={"aa10i" "ac10a" "ba10" "bc20" "ca10" "sex" "cityrur" "edu3" "hinc3rel" "age" "ga10j" "ca10bin"}]; /** create data set **/
     append from S;
     close S;
 QUIT;

My data looks something like this. There are no more columns than the ones you see.

enter image description here

I get the following error message:

590       append from S;
ERROR: Number of columns in S does not match with the number of variables in the data set.

 statement : APPEND at line 590 column 6
591       close S;
NOTE: Cannot close WORK.S; it is not open.
592   QUIT;
NOTE: Exiting IML.

Would someone pretty please tell me what I'm doing wrong?

Upvotes: 1

Views: 583

Answers (1)

vfarmak
vfarmak

Reputation: 13

Reading the error and since I do not have the NY Dataset or test data generated from datastep I assume that the number of variables is not equal to the number of columns.

Hope this helps!

Upvotes: 1

Related Questions