Statalist


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

Re: st: Frequency distribution using multiple loops


From   hmjc66 <[email protected]>
To   [email protected]
Subject   Re: st: Frequency distribution using multiple loops
Date   Fri, 29 Jun 2007 11:31:47 -0400

Nick,

Sorry I was not clearer. This is a survey with 500 participants.

q23-q25 and q29-q31 are categorical variables.
q23-q25 each have 4 categories 1, 2, 3 & 4 (and respondents can choose
more than one). I want to ignore category 4.
q29-q31 each have 6 categories 1, 2, 3, 4, 5 & 6 (and respondents can
choose more than one). I want to ignore category 6.

I am interested in a cross tabulation. For example, if q23 equals 1, I
want to know how many participants chose q29-q31. I need to do the
same for q24 and q25. I also require the sum of the number of
participants who chose 1,2 from q29-q31. (i.e. a participant would be
counted twice if he chose both 1 and 2).

I am using STATA 9.

Do your suggested programs still hold?

Thanks,

Hugh


On 6/29/07, n j cox <[email protected]> wrote:
Hugh's code can be made more transparent.

I delimit with endlines and translate to -forval-
loops. Hugh can do this too unless he is using Stata <7,
in which case he should have told us, following Statalist
FAQ advice.

Each loop is, or should be, indented one tab.

forval i = 1/4 {
       forval j = 1/6 {
               forval k = 23/26 {
                       forval l = 29/32 {
                               count if q`k'==`i' & q`l'==`j';
                               tab q`l' q`k';
                       }
               }
       }
}

That done, this code doesn't seem to match the word description below.

It might help to start explaining upstream of this. Here is
a guess at what you want.

I gather that you have variables

q23-q26

and

q29-q32

abd you want cross-tabulations of pairs, one from each
group.

Your multiple loops show that this is a lot of your work
with your present data structure. I program quite a lot in
Stata, and nesting that many loops is almost never necessary
in my experience. So, change your data structure.

My sense is that you would find things easier if you -reshape-
to long. Suppose there is some identifier -id-. If not,
you can create one:

preserve

keep q23-q26 q29-q32
forval j = 29/32
       rename q`j' Q`j'
}
gen id = _n
reshape long q Q, i(id) j(which)
label var q "q23-q26"
label var Q "q29-q32"
tab q Q

restore

Nick
[email protected]

Hugh wrote

I am trying to get a frequency distribution and have got this far.
Here's what I want to do:- (see program below)

1. I want the sum of the q`l' group (i.e. frequency) if it equals 1 or
2. (i.e. I want the total no of observations if q`l' equals 1 and if
q`l' equals 2).
2. For each q`k', I need the total no of obs of the q`l'. (i.e. For
q23, I need the total no of obs of q29, q30 and q31 for each group
(i.e. 1 and 2 as one group, 3 as one group, 4 as one group, and 5 as
one group)). Same way for q24 and q25.
3. I then want to tabulate the data (table showing the results). My
current tab command is wrong.
4. Is there a more efficient way to run this?

local i=1;
while `i' < 4 {;
local j=1;
while `j' < 6 {;
local k=23;
while `k' < 26 {;
local l=29;
while `l' < 32 {;
       count if q`k'==`i' & q`l'==`j';
       tab q`l' q`k';
local l = `l' + 1;
};
local k = `k' + 1;
};
       local j = `j' + 1;
       };
       local i = `i' + 1;
       };
*
*   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/

*
*   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