Search code examples
sascovariance-matrix

How to generate distinct covariance matrices for each class in SAS?


Using SAS I want to generate the covariance matrices for a dataset which contains a binary class, one covariance matrix for class 1 and one covariance matrix for class 2.

PROC MEANS DATA=my_dataset MAXDEC=2 MEAN STD;
CLASS binary_class;
VAR x1 x2 x3;
RUN;

PROC CORR DATA=my_dataset noprob COV;
CLASS binary_class;
VAR x1 x2 x3;
run;

but when I run the second statement I get:

ERROR 180-322: Statement is not valid or it is used out of proper order.

Can someone please help me?


Solution

  • I don't think PROC CORR has a CLASS statement, you want a BY (#2) statement instead, which does require you sort your data ahead of time.

    You can add an ODS OUTPUT statement (#1) to store the covariance data to a data set.

    proc sort data=my_dataset;
    by binary_class;
    run;
    
    ods output cov=cov_by_binary; /*#1*/
    proc corr data=my_dataset noprob cov;
    by binary_class; /*2*/
    var x1-x3;
    run;
    

    EDIT: In general, a CLASS statement only tells the SAS proc that your variable is categorical, it doesn't necessarily do BY group processing. PROC MEANS is one of the few procs where it can be used somewhat interchangeably.