Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: How to keep if freq of var is >= 100 for both group (Alcohol "1" and non-alcohol "0")?


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: How to keep if freq of var is >= 100 for both group (Alcohol "1" and non-alcohol "0")?
Date   Tue, 25 Sep 2012 11:15:51 +0200

--- On Tue, Sep 25, 2012 at 7:53 AM, Caliph Omar Moumin wrote:
>> I have a large dataset which more than 500,000 observations;
>> and more than 7000 diagnoses,  which is grouped into two groups
>> alcohol coded as "1" and nonlacloh as "0"
>> So i want to keep if observations is  >= 100 for both groups
>> alcohol and nonalcohol based on daignoses. For example if
>> daignoses A120 has more than 100 observations for both alcohol
>> and nonalcohol keep if not drop it.

On Tue, Sep 25, 2012 at 9:31 AM, Nick Cox wrote:
> This is a simple application of -by:-, with which all long-term Stata
> users should be familiar.
>
> bysort diagnosis group : keep if _N > 100

To extend this advise to the case where you want the minimum number of
observations within a diagnosis for alcohol and non-alcohol to be
larger (or equal to) 100 and you have some missing values on some key
variables:

*-------------------- begin example ------------------------
sysuse nlsw88, clear

// Say that variable grade will play an important
// role in some later analysis, so we also need
// to take care of missing values in grade
gen byte touse = !missing(occupation,married,grade)

// diagnosis = occupation
// alcohol = married
// cutoff freqency = 40
bys occupation married touse : gen freq = _N if touse == 1
bys occupation touse (freq) : replace freq = freq[1]
keep if freq >=40

// check
tab occupation married if touse
*-------------------- begin example ------------------------
(For more on examples I sent to the Statalist see:
 http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index