Help using Base SAS procedures

Extracting frequencies from contingency table

Reply
Occasional Contributor
Posts: 12

Extracting frequencies from contingency table

I wish to convert a of the form below  

table

Color          Style

A1          B1

A1          B5

A5          B2

A3          B3

A2          B1

A2          B2

A4          B1

A1          B3

A5          B1

A3          B4

A4          B4

A5          B2

A4          B2

A4          B5

A1          B2

A3          B1

A5          B1

A2          B3

A4          B2

A3          B2

A5          B3

A2          B3

A4          B3

A1          B4

A2          B5

into a contingency table and extract the frequecies into the form of table [ii] below.

Could you please direct me to any material that could help me do this?

"color" "Style" "Count"

"A1" "B1" 4

"A1" "B2" 2

"A1" "B3" 4

"A1" "B4" 1

"A1" "B5" 6

"A2" "B1" 1

"A2" "B2" 3

"A2" "B3" 2

"A2" "B4" 0

"A2" "B5" 4

"A3" "B1" 3

"A3" "B2" 3

"A3" "B3" 1

"A3" "B4" 5

Respected Advisor
Posts: 4,640

Extracting frequencies from contingency table

Tell me if you realy need those precise headers, otherwise, you could use this:

data have;
input color $ style $;
datalines;
A1          B1
A1          B5
A5          B2
A3          B3
A2          B1
A2          B2
A4          B1
A1          B3
A5          B1
A3          B4
A4          B4
A5          B2
A4          B2
A4          B5
A1          B2
A3          B1
A5          B1
A2          B3
A4          B2
A3          B2
A5          B3
A2          B3
A4          B3
A1          B4
A2          B5
;

proc sql;
create table want as
select quote(trim(color)) as qcolor label="color", quote(trim(style)) as qstyle label="Style",
count(*) as count from have
group by calculated qcolor, calculated qstyle;
select * from want;
quit;


PG

PG
Occasional Contributor
Posts: 12

Extracting frequencies from contingency table

Hi  PGStats, Thanks!

Occasional Contributor
Posts: 12

Extracting frequencies from contingency table

Could the expected frequecies have been calculated and extracted similarly?

Respected Advisor
Posts: 4,640

Extracting frequencies from contingency table

Yes they could. But it would be a lot simpler to use proc freq to get those.

PG

PG
Super User
Posts: 17,734

Re: Extracting frequencies from contingency table

As PG mentioned Proc Freq is more efficient.

proc freq data=have;

tables color*style/out=want expected;

run;

Oops...The expected should be outexpect instead.

Occasional Contributor
Posts: 12

Re: Extracting frequencies from contingency table

Thanks, PGStats and Reeza. Is there a way of determining which combinations had zero count as part of proc freq?

Ask a Question
Discussion stats
  • 6 replies
  • 233 views
  • 0 likes
  • 3 in conversation