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.
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;
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.
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.
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;
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.
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!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.