Differences between revisions 12 and 15 (spanning 3 versions)
Revision 12 as of 2023-01-14 20:10:38
Size: 1539
Comment:
Revision 15 as of 2023-06-07 18:54:07
Size: 649
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Stata offers human-readable variable names, variable labels, variable printing formats, and value labels.

Stata stores variable labels and value labels in a separate database. Many built-in commands seamlessly pull in metadata from that database, but there are notable exceptions.
Stata exposes a rich API for querying internal '''metadata'''.
Line 13: Line 11:
== Variable Labels == == Labels ==
Line 15: Line 13:
Variable labels are defined using the '''`label`''' command. Variable and value labels are defined using the '''`label`''' command.
Line 18: Line 16:
label variable foo "bar" label variable foo "yes or no?"
label define yesno 1 "Yes" 0 "No"
label values foo yesno
Line 21: Line 21:
Variable metadata including labels can be inspected and exported using the '''`describe`''' command. See [[Stata/Label|here]] for more options.

Numeric values can be quickly prepended to value labels using the '''`numlabel`''' command.
Line 24: Line 26:
preserve
describe, replace
assert varlab=="bar" if name=="foo"
restore
numlabel foo, add mask("#. ")
Line 34: Line 33:
== Value Labels ==

Variable labels are defined using the '''`label`''' command. However, they must first be defined as a label and then linked to one or more variables.
== Descriptives ==
Line 39: Line 36:
label define baz 0 "No" 1 "Yes"
label define baz 99 "Missing", modify
label values foo baz
}}}

By default, labels cannot be overwritten. The '''`add`''' option allows new values to be inserted into an existing label. The '''`replace`''' option allows existing values to be overwritten in an existing label. The '''`modify`''' option combines both.

Numeric values can be added to value labels using the '''`numlabel`''' command.

{{{
numlabel baz, add mask("#. ")
}}}

----



== Quick Tips ==

Stata has a number of interactive tools for examining metadata.

{{{
di _N // number of cases
di c(k) // number of variables
display _N // number of cases
display c(k) // number of variables

Stata Metadata

Stata exposes a rich API for querying internal metadata.


Labels

Variable and value labels are defined using the label command.

label variable foo "yes or no?"
label define yesno 1 "Yes" 0 "No"
label values foo yesno

See here for more options.

Numeric values can be quickly prepended to value labels using the numlabel command.

numlabel foo, add mask("#. ")


Descriptives

display _N    // number of cases
display c(k)  // number of variables


CategoryRicottone

Stata/Metadata (last edited 2025-10-24 15:58:53 by DominicRicottone)