Stata 15 help for mkassert


[P] mkassert -- Generate asserts for Stata objects


mkassert rclass [, mfmt(fmt) mtol(tol) sfmt(fmt) stol(tol) mwrap(#) name(str) nostripes tmpnames saving(filename [, append | replace])]

mkassert eclass [, mfmt(fmt) mtol(tol) sfmt(fmt) stol(tol) mwrap(#) name(str) nostripes tmpnames saving(filename [, append | replace])]

mkassert sclass [name(str) saving(filename [, append | replace])]

mkassert matrix name-list [, mfmt(fmt) mtol(tol) mwrap(#) name(str) nostripes tmpnames saving(filename [, append | replace])]

mkassert scalar name-list [, sfmt(fmt) stol(tol) name(str) saving(filename [, append | replace])]

mkassert char [name-list] [, dta name(str) saving(filename [, append | replace])]

mkassert obs [varlist] [if] [in] , id(varlist) [computed(spec) name(str) saving(filename [, append | replace])]


mkassert generates a set of assert statements for the current contents of one of the following Stata "objects":

eclass macros, scalars, and matrices in e()

rclass macros, scalars, and matrices in r()

sclass macros in s()

matrix one or more matrices

scalar one or more scalars

char the characteristics associated with the variables in varlist or those associated with the dataset (or both)

obs the values of variables in (a selection of) observations

To enhance readability, the output uses formatting such as alignment and colors. The assert statements can be copied from the Results window into a certification script (see [P] cscript). Alternatively, you can have mkassert write a log file (see [R] log).

If macro texts can be interpreted as integers and reals, asserts are generated that test numerical equality (for reals, after conversion to float). Integer scalar values are tested with strict equality rather than some tolerance.


mfmt(fmt) is the format used to convert matrix elements to ASCII. It defaults to %18.0g.

mtol({#|name|name #}) specifies the tolerance for mreldif testing of matrices. If # is not specified, it defaults to 1e-8. If name is specified, a local macro name is defined equal to #, and all tests in the block refer to name.

sfmt(fmt) is the format used to convert scalars to ASCII. The default is sfmt(%18.0g).

stol({#|name|name #}) specifies the tolerance for reldif testing of scalars. The syntax is described with the mtol() option.

mwrap(#) specifies that mkassert performs element wrapping in generating input commands for a matrix with # or more columns. The default is 4.

name(str) specifies that each assert statement generated is accompanied by a reference str# within a comment and is numbered sequentially.

nostripes specifies that matrix stripes not be included in the set of assert statements.

tmpnames specifies that assert statements include temporary matrices. By default, permanent matrices are created and discarded when no longer needed.

saving(filename [, append | replace]) opens a text-only file with mkassert output to be included in a certification script.

dta specifies that certification statements should be written for the characteristics associated with the dataset _dta.

id(varlist) specifies one or more variables that identify the observations (form a key). These variables are used to match the current observations with those during certification. The if and in clauses are evaluated after the data are sorted on the id() variables.

computed(spec) specifies the real-valued variables that are "computed" and have to be reldif tested rather than equality tested. The specification spec is a varlist with optionally embedded numbers that are the tolerances used in reldif testing for all following variables until overruled by another number. The tolerance defaults to 1e-10.

For example, computed(1e-8 price weight 1e-7 trunk mpg) specifies that price and weight are reldif tested with tolerance 1e-8, and trunk and mpg are tested with tolerance 1e-7.


. sysuse auto (1978 Automobile Data)

. summarize price

Variable | Obs Mean Std. Dev. Min Max -------------+----------------------------------------------------- price | 74 6165.257 2949.496 3291 15906

. return list

scalars: r(N) = 74 r(sum_w) = 74 r(mean) = 6165.256756756757 r(Var) = 8699525.974268789 r(sd) = 2949.495884768919 r(min) = 3291 r(max) = 15906 r(sum) = 456229

. mkassert rclass

assert r(N) == 74 assert r(sum_w) == 74 assert reldif( r(mean) , 6165.256756756757) < 1e-8 assert reldif( r(Var) , 8699525.974268789) < 1e-8 assert reldif( r(sd) , 2949.495884768919) < 1e-8 assert r(min) == 3291 assert r(max) == 15906 assert r(sum) == 456229

If you were writing a certification script for summarize, you would copy the command line and the associated output from mkassert rclass into the script, and possibly delete unimportant asserts. You can, of course, modify the tolerances, but notice the tolerance options to modify the tolerance for all tests, or "parameterize" the tests, via a named constant in the mtol() and stol() options.


mkassert was written by Jeroen Weesie, Department of Sociology, Utrecht University, The Netherlands.

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