*! version 0.31

Title

subselect -- Mark all group ids that satisfy if and in
conditions at least once

Syntax

subselect
varlist
[if]
[in]
[using]
[,
options]

Optional
generate(name) Specify name of generated marker variable.
One of options generate(name), keep, or drop must be set

clear In combination with using. Clears data editor

replace Replace the marking variable named in generate(name)

negate Negate the marking

keep Keep if at least one marking.
One of options generate(name), keep, or drop must be set.
No marking variable is created

drop Drop if at least one marking.
One of options generate(name), keep, or drop must be set.
No marking variable is created

Description

Description

subselect generates a marker variable with name specified by option
generate(name). The grouping variable is specified the varname argument.
The marker marks all group ids, where at least one row per group id satisfies
the conditions in if and in.

Examples

Example data:

`"use http://www.stata-press.com/data/r15/nlswork.dta, clear"'

The nlswork data frame contains data about 4711 young working women who
had an age of 14–26 years in 1968.
These data are collected within the "National Longitudinal Survey" over the
years 1968-1988 (with gaps).
There are 28534 observations in total.
The unique keys are variables idcode and year.

Mark all persons (idcode) that at some row in the dataset has been minor (age 18 or below):

`"subselect idcode if age <= 18, gen(age18)"'

To see the generated variable for the first 2 person ids:

`"list idcode age age18 if idcode < 3, noobs sepby(idcode)"'

The person with idcode 1 has age 18 at start and is marked whereas the person
with idcode 2 do not age 18 at any time and hence is not marked.

Using sumat to see unique number of persons at different ages:

`"sumat idcode, statistics(unique) rowby(age) decimals(0)"'

To see the unique number of persons who has been minor in the dataset at least once:

`"sumat idcode if age18, statistics(unique) rowby(age) decimals(0)"'

By restricting the to the persons who are minor at some age some of the
higher ages are excluded.

To see unique number of persons who never has been minor in the dataset at different ages:

`"sumat idcode if !age18, statistics(unique) rowby(age) decimals(0)"'

To keep all rows for persons by idcode having no missing registration
of the variable union

//www.stata-press.com/data/r15/nlswork.dta", clear negate keep"'

Now we only have persons with no missing registration of union

`"codebook union"'

To drop all rows for persons by idcode who never have been maried in the
period from year 68 to year 88.

//www.stata-press.com/data/r15/nlswork.dta", clear negate drop"'

To see the selected rows for the first 2 person ids:

`"list idcode msp if inlist(idcode, 1, 2), noobs sepby(idcode)"'

The person with idcode 1 has been married in the years 71 to 75 whereas
person with idcode 2 have been married in the years 71 to 83.

Authors and support

Author:

Niels Henrik Bruun,

Aalborg University Hospital

Support:

niels.henrik.bruun@gmail.com