Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: counting


From   Philip Ryan <philip.ryan@adelaide.edu.au>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: counting
Date   Sat, 7 Feb 2004 22:55:32 +1030

It is hard to answer this question without a much better idea of (i) how your 
data set is constructed and (ii) just what it is that you want.

Chris asks, quite reasonably, whether you just want a visual display, because 
you imply (?) that you do not want to store the results for future use (".. 
without generating a new variable").  But your own solution:

 by state: count if child==1

will do a perfectly good job of simply displaying _a_ result for each state 
(whether it is the result you want depends on your data setup).

Perhaps you do wish to store the results?  If so you have a number of choices: 
a single variable, a series of macros or scalars, or as a vector.

Here is one approach:

gen num_kids = .
levels state, local(slevels)
foreach s of local slevels {
count if state ==`s' & child == 1
replace num_kids = `r(N)' if state ==`s'
}

... and then you can play around with the variable num_kids.

Note that this depends on (i) state being a numeric variable (if it is a 
string, then either first -encode- it or else enclose the two instances of `s' 
in my code with double quotes: "`s'") and, especially, (ii) on the meaning of 
the child variable in your data. 

Whether the specific code above is correct for your situation is probably less 
relevant than that -help levels- and -help foreach-  are often good places to 
start in problems like these.

Phil



Quoting chris wallace <c.wallace@qmul.ac.uk>:

> Quoting Amit Khandelwal <amit.khandelwal@yale.edu>:
> 
> > Suppose I have 5 states and I want to count the number of children in each
> 
> > state. How can I do this without generating a new variable. If I do
> > 
> > by state: count if child==1
> > 
> > then r(N) only stores the last state's result.
> 
> If you just want a visual display?
> 
> tab state if child==1
> 
> C.
> *
> *   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/
> 


-- 
Philip Ryan
Associate Professor
Department of Public Health
University of Adelaide
5005 South Australia
AUSTRALIA
CRICOS Provider Number 00123M
----------------------------------------------------------- 
This email message is intended only for the addressee(s) 
and contains information that may be confidential and/or 
copyright.  If you are not the intended recipient please 
notify the sender by reply email and immediately delete 
this email. Use, disclosure or reproduction of this email 
by anyone other than the intended recipient(s) is strictly 
prohibited. No representation is made that this email or 
any attachments are free of viruses. Virus scanning is 
recommended and is the responsibility of the recipient.
*
*   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