|
Size: 1800
Comment:
|
← Revision 16 as of 2025-10-24 15:58:53 ⇥
Size: 942
Comment: Rewrite
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| ## page was renamed from StataMetadata = Metadata = |
= Stata Metadata = |
| Line 4: | 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 14: | Line 11: |
| == Variable Names == | == Labels == |
| Line 16: | Line 13: |
| Variables are (re)named using the `rename` command. Since Stata 12, it offers some advanced features: | Variable and value labels are primarily manipulated by the [[Stata/Label|`-label-`]] command. |
| Line 19: | Line 16: |
| // rename all variables with a leading 'v' such that the 'v' is replaced with a 'V' rename v* V* |
. label variable foo "yes or no?" |
| Line 22: | Line 18: |
| // rename all variables with a leading 'new_' such that the prefix is stripped rename new_* * |
. label define yesno 1 "Yes" 0 "No" |
| Line 25: | Line 20: |
| // rename all variables to a lowercase name rename *, lower |
. label values foo yesno . label list yesno yesno: 0 No 1 Yes |
| Line 29: | Line 28: |
| For older versions of Stata, try these: | Numeric values can be quickly prepended to value labels using the '''`-numlabel-`''' command. |
| Line 32: | Line 31: |
| renpfix v V renpfix new_ // TODO: how did older versions rename variable to lowercase? |
numlabel foo, add mask("#. ") }}} To access or store the text of a variable label, try: {{{ local varlab_foo: variable label foo }}} To manipulate value labels, try: {{{ local vallab_foo: value label foo local vallab_foo_1: label `vallab_foo' 1, strict |
| Line 41: | Line 51: |
| == Labels == === Variable Labels === |
== Descriptives == |
| Line 46: | Line 54: |
| label variable VAR "var" }}} === Value Labels === Value labels are defined separately from the actual dataset. {{{ label define LABEL 0 "No" 1 "Yes" label define LABEL 99 "Missing", modify label values VAR LABEL }}} 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 LABEL, 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.
Contents
Labels
Variable and value labels are primarily manipulated by the `-label-` command.
. label variable foo "yes or no?"
. label define yesno 1 "Yes" 0 "No"
. label values foo yesno
. label list yesno
yesno:
0 No
1 YesNumeric values can be quickly prepended to value labels using the -numlabel- command.
numlabel foo, add mask("#. ")To access or store the text of a variable label, try:
local varlab_foo: variable label foo
To manipulate value labels, try:
local vallab_foo: value label foo local vallab_foo_1: label `vallab_foo' 1, strict
Descriptives
display _N // number of cases display c(k) // number of variables
