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

Re: st: counting


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: counting
Date   Sat, 7 Feb 2004 06:54:40 -0600

Another way would be to use -tabstat-

Something like:

tabstat child if child == 1, by(state) stat(N) save


Scott

----- Original Message ----- 
From: "Philip Ryan" <philip.ryan@adelaide.edu.au>
To: <statalist@hsphsun2.harvard.edu>
Sent: Saturday, February 07, 2004 6:25 AM
Subject: Re: st: counting


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

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