BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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:

 

 

 

 

5 REPLIES 5
Ksharp
Super User
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;

Ksharp_0-1728541934202.png

 

 

"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;
Ronein
Meteorite | Level 14

Thank you so much.
So as I understand only one change is needed to change  chisq to exact chisq.

(What is the difference in the statistical names? I want to ready the theory)

 

About my question how to improve running time of kramer matrix. 

What is the way?

Ksharp
Super User
Yes. But that would cost you lots of time if you have many obs.
Cramer's V statistic should be the same .but ChiSquare Test would be replace by Exact Fisher Test.
If you want its theory/algorithm ,check PROC FREQ 's documentation.

"how to improve running time of kramer matrix. "
What do you mean by that ? PROC FREQ calculated Cramer's V statistic should have CONSTANT /SAME time interval once the obs number is specified .
Mine would be faster than yours due to not use macro statement.
If you really want to get faster ,then you need write your own code(or IML code) to calcuate Cramer's V statistic. and @Rick_SAS might have an interesting to do that.
Ronein
Meteorite | Level 14

Thanks

The question is by using my code ( user define the variables via macro variable) how to cause the code to calculate only one time the correlation between 2 variables 

(In my code for example calculated correlation between x  and y and then between y and x   but they are same...)

 

Rick_SAS
SAS Super FREQ

You seem to be asking about how to write a macro loop that iterates over (i,j) pairs where i <= j.  Look at this example. Hope it helps.

 

 

%let n = 4;
%macro TRI;
%do i = 1 %to &n.;
   %do j = &i. %to &n.;
      %put &=i, &=j;
   %end;
%end;
%mend;
%TRI;

The key is to start to J loop at &i instead of at 1.

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 122 views
  • 0 likes
  • 3 in conversation