Announcement

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

  • Label variable list (varlist) based on labels of another varlist

    I want to label a list of variables using the same labels as another list of variables. In the table below, I want to
    1) first label journal1-4 using the first columns (successful in my code (1))
    2) then write a code to "copy" journalname1-journalname4 to the second variable list (or directly label the variables aboutjournal1-4 with the same labels)
    journal1 journal2 journal3 journal4 aboutjournal1 aboutjournal2 aboutjournal3 aboutjournal4
    journalname1 journalname2 journalname3 journalname4 - - - -
    ... ... ... ... ... ... ... ..
    *** code (1)
    Code:
    unab varlist : journal1-journal4 
    foreach v of local varlist {
        local value = `v'[1]
        local vname = strtoname(`"`value'"')
        rename `v' `vname'
        label var `vname' `"`value'"'
    }
    How can this be done simply?

  • #2
    I think you're looking for something like

    Code:
    foreach v of var journal1-journal4 { 
          local vlabel : var label `v' 
          label var about`v' "`vlabel'" 
    }

    Comment


    • #3
      Excellent.
      For the interested reader, a summary solution to the entire problem (that also works) is:
      Code:
      unab varlist : journal1-journal40 
      foreach v of local varlist {
          local value = `v'[1]
          label var `v' `"`value'"'
          label var about`v' "`value'" 
      }

      Comment


      • #4
        The entire problem is likely also to entail

        Code:
        drop in 1
        followed by an application

        Code:
        destring
        Datasets often arrive like this -- especially if they have been anywhere near MS Excel -- but they still require this kind of rocket surgery before they are of much use within Stata.

        Comment

        Working...
        X