Fluorite | Level 6

## 3 way cross tabulation (chi square test)

I am trying to write code in SAS University edition to do a 3-way cross tabulation. For example, if I have 3 categorical variables and I want to look at presisting condition status(preex_status) and insurance coverage(coverage_status) by year whether before or after the policy went into effect (year_class). Each variable has two categories the year_Class has 'pre' and 'post' and the other two have 0 and 1 for yes or no response. I wrote following code:

proc freq data = mydata;

tables preex_status*coverage_status*year_Class / chisq;

run;

However it gives a 2*2 table controlling for preex_status. I cannot use the 'By' statement gives me an error saying "data is not sorted in ascending sequence. I was expecting a table as below:

 Pre Post Coverage status Pre-existing condition No Pre-existing condition No Yes No

Is this possible or am I using the wrong statistical test? Can someone help me solve it? Thank yo

1 ACCEPTED SOLUTION

Accepted Solutions
Fluorite | Level 6

## Re: 3 way cross tabulation (chi square test)

Thanks but that won't work. But i think i might have solved it. I just created a new variable using condition statement and created four categories for the new variable where 1. a person has condition and insurance. 2. Has condition but no insurance 3. No condition but has insurance 4. no condition and no insurance then used this new variable for a two way table with year variable using code:

`proc freq data=mythesis.jdata ;  tables condition_coverage*year_class/ chisq;  weight perweight1;run;`

and got following output:

 Table of condition_coverage by year_class condition_coverage year_class FrequencyPercentRow PctCol Pct post pre Total nocond_hascovera 3.364E721.2651.8542.74 3.124E719.7448.1539.28 6.488E741.01 nocond_nocoverag 58186153.6837.697.39 96189866.0862.3112.10 1.544E79.76 preex_nocoverage 34921392.2136.034.44 61989153.9263.977.80 96910536.13 prex_hascoverage 3.575E722.6052.4145.43 3.246E720.5247.5940.82 6.821E743.11 Total 7.87E749.74 7.951E750.26 1.582E8100.00

Looks like that solves the question. Will update once i get it verified if this solution is correct. Thanks again.

6 REPLIES 6
Lapis Lazuli | Level 10

## Re: 3 way cross tabulation (chi square test)

Fluorite | Level 6

## Re: 3 way cross tabulation (chi square test)

Hi, thank you for the reply. I have tried that solution but does not work in my case. It gives me an error message that, "data is not sorted in ascending sequence'. Thanks.

Lapis Lazuli | Level 10

## Re: 3 way cross tabulation (chi square test)

Try running this prior to proc freq:

``````proc sort data=mydata;
by preex_status coverage_status year_Class;
run;``````
-unison
Fluorite | Level 6

## Re: 3 way cross tabulation (chi square test)

Thank you @unison that definitely helped and I could run the code. But i still got 2 tables. Is there anyway i could get one table like below:

 Before Policy after policy Coverage status Pre-existing condition No Pre-existing condition No Yes No

Thank you again 🙂

Lapis Lazuli | Level 10

## Re: 3 way cross tabulation (chi square test)

What do you want to go in the blank spots?

if it’s just the frequencies then you can use

``out=outfreq``

On the table statement (next to your chisq option). Then you would do something like this:

``````proc report data=outfreq;
column sex height, count weight,count;
define sex/group;
define height/across;
define weight/across;
define count/analysis sum;
run;``````
-unison
Fluorite | Level 6

## Re: 3 way cross tabulation (chi square test)

Thanks but that won't work. But i think i might have solved it. I just created a new variable using condition statement and created four categories for the new variable where 1. a person has condition and insurance. 2. Has condition but no insurance 3. No condition but has insurance 4. no condition and no insurance then used this new variable for a two way table with year variable using code:

`proc freq data=mythesis.jdata ;  tables condition_coverage*year_class/ chisq;  weight perweight1;run;`

and got following output:

 Table of condition_coverage by year_class condition_coverage year_class FrequencyPercentRow PctCol Pct post pre Total nocond_hascovera 3.364E721.2651.8542.74 3.124E719.7448.1539.28 6.488E741.01 nocond_nocoverag 58186153.6837.697.39 96189866.0862.3112.10 1.544E79.76 preex_nocoverage 34921392.2136.034.44 61989153.9263.977.80 96910536.13 prex_hascoverage 3.575E722.6052.4145.43 3.246E720.5247.5940.82 6.821E743.11 Total 7.87E749.74 7.951E750.26 1.582E8100.00

Looks like that solves the question. Will update once i get it verified if this solution is correct. Thanks again.

Discussion stats
• 6 replies
• 693 views
• 1 like
• 2 in conversation