*! version 1.2.0 15aug2025
help markobs

Title

[Community-contributed] markobs -- Mark observations for inclusion

Syntax

Create new marker variable

markobs newmarkvar
[if] [in] [weight]
[, zeroweight mark_options]

Modify existing marker variable

markobs markvar varlist [,
strok
sysmissok
markout_options]

aweights, fweights, pweights, and iweights are
allowed; see weight.

Time-series operators are allowed; see tsvarlist.

Description

markobs
is a wrapper for official Stata's
mark
and
markout
commands.
It creates and modifies a 0/1 to-use variable
that records which observations are to be used in subsequent code.

The syntax diagrams reflect the workflow.
The first syntax creates a new 0/1 to-use variable, newmarkvar,
and sets it to 0 for observations
that do not satisfy the if expression,
fall outside the in range,
or have a weight of zero
(see rules 1--4 and 7 in Remarks below).
No varlist is allowed.

Subsequent calls to markobs further restrict observations
by setting an existing 0/1 to-use variable to 0
if any of the variables in varlist are strings
or contain missing values (see rules 5 and 6 in Remarks below).
markobs
will exit with an error
if the specified marker variable, markvar, was not created by markobs.
This behavior reduces the risk of unintentionally overwriting an existing variable
when the marker variable name is omitted or misspecified.

Options

zeroweight
deletes rule 1 in Remarks below,
meaning that observations will not be excluded because the weight is zero.

strok
specifies that string variables in varlist are to be allowed.
strok changes rule 6 in Remarks below to read

"The marker variable is set to 0 in observations for which any of
the string variables in varlist is empty (contain "")."

sysmissok
specifies that numeric variables in varlist
equal to system missing (.) are to be allowed
and only numeric variables equal to extended missing (.a, .b, ...)
are to be excluded.
The default is
that all missing values (., .a, .b, ...) are excluded.

mark_options
are options used with the mark command.

markout_options
are options used with the markout command.

Remarks

When you use markobs, the following rules apply:

1. The marker variable is set to 0 in observations for which weight
is 0 (but see option zeroweight).

2. The appropriate error message is issued, and everything stops if
weight is invalid (such as being less than 0 in some observation or
being a noninteger for frequency weights).

3. The marker variable is set to 0 in observations for which the if
exp is not satisfied.

4.
The marker variable is set to 0 in observations outside the in
range.

5. The marker variable is set to 0 in observations for which any of the
numeric variables in varlist contain a numeric missing value.

6. The marker variable is set to 0 in all observations if any of the
variables in varlist are strings; see option strok for
an exception.

7. The marker variable is set to 1 in the remaining observations.

Example

Setup

. sysuse auto

Create a marker variable, touse, indicating foreign cars

. markobs touse if foreign == 1

Modify the marker variable
to exclude observations with missing values on rep78

. markobs touse rep78

Acknowledgments

markobs
builds on an idea suggested by Dirk Enzmann on
Statalist

Support

Daniel Klein

klein.daniel.81@gmail.com

Also see

Manual: [P] mark

Help: [P] mark