Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Cross-tabulation of data


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Cross-tabulation of data
Date   Sun, 1 Jul 2007 16:39:11 +0100

clear 

input id q23 q24	q25	q29	q30	q31 
1	3	1	.	1	4	.
2	4	2	3	5	3	2
3	2	.	.	2	.	.
4	3	.	.	3	.	.
5	1	3	2	4	6	1
end 

matrix results = J(4,3,.) 

qui forval i = 1/3 { 
	count if q23==`i' & (q29==1 | q29==2)
	local a = r(N) 
	count if q24==`i' & (q30==1 | q30==2)
	local a = `a' + r(N) 
	count if q25==`i' & (q31==1 | q31==2)
	matrix results[1,`i'] = `a' + r(N) 

	count if q23==`i' & q29==3
	local b = r(N) 
	count if q24==`i' & q30==3
	local b = `b' + r(N) 
	count if q25==`i' & q31==3
	matrix results[2,`i'] = `b' + r(N) 

	count if q23==`i' & q29==4
	local c = r(N) 
	count if q24==`i' & q30==4
	local c = `c' + r(N) 
	count if q25==`i' & q31==4
	matrix results[3,`i'] = `c' + r(N) 

	count if q23==`i' & q29==5
	local d = r(N) 
	count if q24==`i' & q30==5
	local d = `d' + r(N) 
	count if q25==`i' & q31==5
	matrix results[4,`i'] = `d' + r(N) 
} 

matrix colnames results = NP FP G 
matrix rownames results = <25% 25-49% 50-74% >74% 
matrix li results, noheader 

Nick 
[email protected] 

hmjc66
 
> I posted my question yesterday, but realized there were some errors as
> Nick rightly pointed out. Here is another attempt at what I am trying
> to do.
> 
> Assume my data is as follows:-
> 
> id	q23	q24	q25	q29	q30	q31
> 1	3	1		1	4	
> 2	4	2	3	5	3	2
> 3	2			2		
> 4	3			3		
> 5	1	3	2	4	6	1
> 
> q23-q25 and q29-q31 are categorical variables.
> q23-q25 each has 4 categories 1, 2, 3 & 4. I want to ignore 
> category 4.
> q29-q31 each has 6 categories 1, 2, 3, 4, 5 & 6. I want to 
> ignore category 6.
> 
> I am interested in a cross tabulation.
> 
> Here's what I need:-
> 
> count if q23==1 & (q29==1 | q29==2);
> count if q24==1 & (q30==1 | q30==2);
> count if q25==1 & (q31==1 | q31==2);
> 
> /* I then need to sum these */; Lets call the total "a"
> 
> count if q23==1 & q29==3;
> count if q24==1 & q30==3;
> count if q25==1 & q31==3;
> 
> 
> /* I then need to sum these */; Lets call the total "b"
> 
> 
> count if q23==1 & q29==4;
> count if q24==1 & q30==4;
> count if q25==1 & q31==4;
> 
> /* I then need to sum these */; Lets call the total "c"
> 
> count if q23==1 & q29==5;
> count if q24==1 & q30==5;
> count if q25==1 & q31==5;
> 
> /* I then need to sum these */; Lets call the total "d"
> 
> 
> THEN GO THROUGH THE SAME PROCEDURE using q23=2, q24=2 & q25=2. Assume
> the four values generated are e, f, g and h
> 
> FINALLY DO THE SAME using q23=3, q24=3 & q25=3. Assume the four values
> generated are i, j, k and l
> 
> 
> I finally need the results in a table such as
> 
> 	NP	FP	G
> < 24%	a	e	i
> 25-49%	b	f	j
> 50-74%	c	g	k
> >75%	d	h	l
> 
> I am using STATA 9.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index