Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • extract variable labels for new variable names

    Hi there

    I try to automate my programming as much as possible and one challenge I've come up against recently is in trying to name new variables according to the value labels of existing variables.

    For example:

    Code:
    sysuse sandstone
    tab type, gen(type_name)
    My goal is to name the new variables:
    type_measured
    type_estimated
    type_interpolated

    Any advice greatly appreciated.

  • #2
    The extended macro functions (see -help extended functions-) enable you to obtain variable labels from variables, so proceeding using -tab- to create the variables, you could do this:
    Code:
    sysuse sandstone, clear
    tab type, gen(type_name)
    foreach t of varlist type_name* {
       local name: variable label `t'
       local name = substr("`name'", strpos("`name'", "==") + 2, .)
       rename `t' `name'
    }
    However, if you created the indicator variables yourself, you could accomplish the same thing, perhaps more easily, as follows:
    Code:
    levelsof type, local(typelevels)
    foreach v of local typelevels {
       local name: label (type) `v'
       gen `name' = (type == `v') if !missing(type)
    }

    Comment

    Working...
    X