Statalist


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

st: RE: Conformability error in creating matrix while cross-tabulating data


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Conformability error in creating matrix while cross-tabulating data
Date   Thu, 5 Jul 2007 16:40:00 +0100

Consider what happens first time round the loop. 

i is set to 271 and you ask Stata to put a value 
in results[271, 1]. As you only set up a matrix
with 7 rows, this won't work. You don't need
to -rename- your variables, you just need

local I = `i' - 270
matrix results[`I', 1] = ... 

and so forth. 

Nick 
n.j.cox@durham.ac.uk 

Hugh Colaco
 
> I am trying to get the frequency of certain variables and put it in a
> matrix (see code below). I need to cross tabulate each variable
> q271-q277 with each variable q256-q260. I then need to add the
> frequencies of q256-q258 as one group and q259-q260 as the other
> group. Finally, I want to create a 7 x 2 matrix of the results
> (q271-q277 will be the 7 rows; and q256-q258 & q259-q260 will be the 2
> columns).
> 
> I ran the following code, but am getting "conformability error". I
> think the problem is that I want a 7 x 2 matrix, but I'm using i =
> 271/277 rather than i=1/7. If this is the case, can you suggest an
> alternative?
> 
> P.S. - Right now, I have variables q1-q7. I tried to drop them and
> rename q271-q277 as q1-q7 (in case this was the issue), without much
> luck.
> 
> Thanks,
> 
> Hugh
> 
> 
> matrix results = J(7,2,.);
> 
> forval i = 271/277 {;
> 	count if q`i'~=. & q256~=.;
>       local a = r(N);
>       count if q`i'~=. & q257~=.;
>       local a = `a' + r(N);
>       count if q`i'~=. & q258~=.;
> 	matrix results[`i',1] = `a' + r(N);
> 
>      	
>        count if q`i'~=. & q259~=.;
>        local b = r(N);
>        count if q`i'~=. & q260~=.;
>        matrix results[`i',2] = `b' + r(N);
> 
> };
> 
> matrix colnames results = NP FP;
> matrix rownames results = q271 q272 q273 q274 q275 q276 q277;
> matrix li results, noheader;

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index