## 3-way cross-tabulations (chi-square tests)

Solved
Occasional Contributor
Posts: 13

# 3-way cross-tabulations (chi-square tests)

I am trying to write code in SAS to do a 3-way cross tabulation. For example, if I have 3 categorical variables and I want to look at gender and incident by schooltype.

I wrote code as follows:

proc freq data = mydata;

tables gender*incident*schooltype / chisq;

run;

However, the output only shows a 2x2 table and gender was controlled for.

Can you help me figure out the correct code? Thank you.

Accepted Solutions
Solution
‎10-31-2011 05:13 PM
PROC Star
Posts: 7,646

## 3-way cross-tabulations (chi-square tests)

I'm still not sure what you are trying to do.  Is is something like the following?:

data test;

set sashelp.class;

if height gt 62 then height=1;

else height=0;

if weight gt 99 then weight=1;

else weight=0;

run;

proc sort data=test;

by sex;

run;

proc freq data=test ;

tables height*weight/ chisq measures cmh;

by sex;

run;

All Replies
PROC Star
Posts: 7,646

## 3-way cross-tabulations (chi-square tests)

That is the way SAS outputs a 3 or more way crosstabulation.  If you wanted to achieve a different output you would have to design your own using something like proc report or proc tabulate.

Occasional Contributor
Posts: 13

## 3-way cross-tabulations (chi-square tests)

I looked at proc tabulate and it's hard to figure out. I'm trying to understand which variable is designated as the control variable - is it the meta variable, i.e., schooltype? Here's some code I found:

proc freq data=kali.chks4 order=formatted ;

tables schooltype*(gender incident)/ noprint chisq measures cmh;

run;

I simply want to see what gender*incident looks like by school type.

Solution
‎10-31-2011 05:13 PM
PROC Star
Posts: 7,646

## 3-way cross-tabulations (chi-square tests)

I'm still not sure what you are trying to do.  Is is something like the following?:

data test;

set sashelp.class;

if height gt 62 then height=1;

else height=0;

if weight gt 99 then weight=1;

else weight=0;

run;

proc sort data=test;

by sex;

run;

proc freq data=test ;

tables height*weight/ chisq measures cmh;

by sex;

run;

Occasional Contributor
Posts: 13

## 3-way cross-tabulations (chi-square tests)

This is what I needed. I needed to run the cross tab by the school type. The output is kind of strange though. It gave me four tables, not two, with two tables containing output for one school type, and two tables containing output for another  school type. I wonder if my dataset is too large for one table? In any event, the code you gave me was great, so thank you very much.

🔒 This topic is solved and locked.