help trackobs

Title

trackobs -- Keep track of number of observations

Syntax

Basic syntax (required)

trackobs set [ , clear ]

trackobs [:] command

where command is a Stata command

Advanced syntax (optional)

trackobs saving filename [ , replace ]

trackobs report [ using filename ]

trackobs clear

Description

trackobs keeps a record of the number of observations in the
current dataset. The syntax diagram represents the typical workflow:

trackobs set specifies that the number of observation is to be
recorded. Technically, the command defines a characteristic,
_dta[trackobs_counter], that is used by trackobs; see
char.

trackobs, when specified as a prefix, records the number of
observations before command is executed and after command
has concluded; command itself is recorded, too. The full syntax is

trackobs [[ , return ] : ] [ command ]

where the colon, following trackobs, must be typed if option
return is specified or if command is also a trackobs
subcommand. Technically, trackobs defines a characteristic,
_dta[trackobs_i], where i is the counter that
is stored in _dta[trackobs_counter]; see char.

trackobs saving saves the recorded commands and associated numbers of
observations to a Stata dataset. This is useful when you want to manipulate
the recorded information. Concering workflow, trackobs saving is
optional; trackobs results are saved along with the dataset when
they are not cleared.

trackobs report lists the recorded commands and associated numbers of
observations, optionally from filename. When filename is not
specified, trackobs creates a temporary file from chracteristics; see
char.

trackobs clear discards all records. Records are saved with the
dataset when they are not cleared. Technically, the command deletes
the contents of all characteristics _dta[trackobs_*]; see
char.

trackobs might fail when command modifies the _dta
characteristics; see char. Some commands, such as
preserve and restore, do not work correctly when
they are prefixed with trackobs.

Options

clear clears previous trackobs results and resets the
counter.

return returns the number of observations before command was
executed, the number of observations when command concluded, and
command itself in r(). When command is not specified,
the last recorded command and the associated numbers of observations
are returned.

replace allows trackobs to overwrite filename.

Examples

. trackobs set

. trackobs : sysuse auto

. trackobs : drop if foreign

. trackobs report

. trackobs clear

Saved results

trackobs stores its results in characteristics; see char.

trackobs with the return option saves the
following in r():

Scalars

r(N_was) number of observations before command
was executed

r(N_now) number of observations when command
concluded

Macros

r(cmdline) command as typed

Acknowledgments

trackobs was first published on Statalist as an answer to a
request from an anonymous poster.

Author

Daniel Klein, INCHER-Kassel, University of Kassel, klein.daniel.81@gmail.com

Also see

Online: log, char, list