Hello
I want to run a code that calculate Kramer correlation matrix between categorical variables.
I have a question please about improving the code.
My code check correlation between any pair of variables
In my example there are 4 variables X Y Z R so it calculate correlation between:
X,X X,Y X,Z X,R
Y,Y Y,X Y,Z Y,R
Z,Z Z,X Z,Y Z,R
R,R R,X R,Y R,Z
So it calculate 16 correlations
However, correlation of X,Y is same as correlation of Y,X (and so on)
How can we improve the code that it will calculate the correlation matrix quicker?
Another question-
I have a warning message -
WARNING: 31% of the cells have expected counts less than 5, for the table of X by _X
Chi-Square may not be a valid test.
What is the way to solve it?
/****categorical Variables list****/
%let list1var=X Y Z R;
%let list2var=&list1var.;
/**Number of varaibles***/
%let n =%sysfunc(countw(&list1var));
%put n=&n;
%macro CRAMV;
%do i = 1 %to &n.;
%let var1=%scan(&list1var.,&i.,+);
%do j = 1 %to &n.;
%let var2=%scan(&list2var.,&j.,+);
ods select none;
PROC FREQ DATA=Raw_Data_tbl;
TABLES &VAR1.* (&VAR2.)/CHISQ MISSING NOPERCENT NOCOL;
OUTPUT OUT=ttt CRAMV ;
RUN;
DATA want;
RETAIN VAR1 VAR2;
SET ttt;
LENGTH VAR1 VAR2 $32.;
VAR1 ="&VAR1";
VAR2 ="&VAR2";
RUN;
PROC APPEND DATA=want BASE=Kramer_All force;quit;
%end;
%end;
%mend;
%CRAMV;
/***Create matrix***/
title;
ods select all;
proc tabulate data=Kramer_All;
class VAR1 VAR2;
var _CRAMV_;
table (VAR1=''),VAR2=''*(_CRAMV_=''*min=''*f=6.5 );
run;
but it also calculate correlation between:
ods select none;
ods output ChiSq= ChiSq;
PROC FREQ DATA=sashelp.heart(obs=1000);
TABLES (sex status bp_status)*(sex status bp_status)/CHISQ MISSING NOPERCENT NOCOL;
RUN;
ods select all;
data ChiSq2;
set ChiSq(where=(Statistic="Cramer's V"));
row=scan(table,-2,' *');
col=scan(table,-1,' *');
run;
proc tabulate data=ChiSq2 ;
class row col;
var value;
table row='',col=''*value=''*sum=''*f=6.3;
run;
"WARNING: 31% of the cells have expected counts less than 5, for the table of X by _X
Chi-Square may not be a valid test."
Yes. You need EXACT Chisquare Test , Like:
PROC FREQ DATA=sashelp.heart(obs=1000);
TABLES sex *status/CHISQ MISSING NOPERCENT NOCOL ;
exact chisq ;
RUN;
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 use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.