Programming the statistical procedures from SAS

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

Accepted Solution Solved
Reply
Occasional Contributor kvc
Occasional Contributor
Posts: 13
Accepted Solution

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
Esteemed Advisor
Posts: 7,056

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;

View solution in original post


All Replies
Esteemed Advisor
Posts: 7,056

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 kvc
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
Esteemed Advisor
Posts: 7,056

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 kvc
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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 1692 views
  • 0 likes
  • 2 in conversation