Announcement

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

  • Labels (Abc) from variable names (Abc_defghi)

    Hi all,

    I've seen a few posts asking questions about labelling variables by their respective names, but I haven't come across a way to address my problem:
    I'm working with a few datasets I'm trying to merge using a year variable. The variables are very similar in that they share country names. I'd like to label all of the variables that begin with country names by their country names. I've been looking at using split, but can't figure it out.

    other potentially pertinent info:

    variables names are in the following format: Abc_Defghi ; (ideally this variable would be labelled as Abc)

  • #2
    Try something along the lines of
    Code:
    foreach var of varlist _all {
        local place = strpos("`var'", "_")
        if !`place' continue
        label variable `var' `=substr("`var'", 1, `place'-1)'
    }
    Last edited by Joseph Coveney; 03 Oct 2019, 02:02.

    Comment


    • #3
      That seems to have done the trick.

      Thanks Joseph!

      Comment

      Working...
      X