Stata: Retrieve variable label in a macro

5.5k views Asked by At

I'm generating graphs for several variables using a do-file, I would like to be able to retrieve a variable label (so that I could use it for the graph title).

In my dreams, something along those lines:

sysuse auto, replace
local pricelabel = varlab(price)
display "Label for price variable is `pricelabel'"

which would (in my dreams) yield:

Label for price variable is Price

I looked in the documentation of describe, summarize, as well as the part on labels, but could not seem to find a solution.

1

There are 1 answers

0
Brendan On BEST ANSWER

See the help files for extended functions of local/global macros: help extended_fcn (where extended_function is variable label varname [what you asked for] or value label varname [not what you asked for, but may be of use]). E.g.:

sysuse auto, clear
local x : variable label foreign
local y : value label foreign
di "`x'"
di "`y'"

returns

. sysuse auto, clear
(1978 Automobile Data)

. local x : variable label foreign

. local y : value label foreign

. di "`x'"
Car type

. di "`y'"
origin