Bookmark and Share

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

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

Re: st: Creating a data subset with subjects chosen at random

From (Brendan Halpin)
Subject   Re: st: Creating a data subset with subjects chosen at random
Date   Fri, 15 Jun 2012 14:29:30 +0100

On Fri, Jun 15 2012, Amal Khanolkar wrote:

> - I would like to create a subset of the above dataset that consists of
> 1. 100,000 subjects, 2. With the same distribution of subjects by
> country of origin as above in the parent dataset. 3. Tell Stata to
> choose the subjects at random. The dataset of course has several other
> variables. But I would like to define the new data subset based on the
> above country of origin as it is my main exposure variable.

Something like this

// create random variable
gen x = runiform()

// sort by country and x
sort cbirth x

// create a variable within country identifying the first 5% (change
// this proprtion as you wish)

by cbirth: gen subsamp = _n <= (_N+0.5)*0.05

tab cbirth subsamp, col

tab cbirth sex if subamp


Brendan Halpin,   Department of Sociology,   University of Limerick,   Ireland
Tel: w +353-61-213147  f +353-61-202569  h +353-61-338562;  Room F1-009 x 3147    ULSociology on Facebook:         twitter:@ULSociology
*   For searches and help try:

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