Announcement

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

  • Using variable labels as matrix rownames

    Dear Statalists,

    I have a large set of labelled variables and I would like to use these labels as the rownames of a matrix. Importantly, the labels consist of several words, which is causing a problem with my present solution.

    What I have so far is the following:

    sysuse auto, clear

    local rows
    foreach var of varlist m* {
    local labels: variable label `var'
    local rows `rows' `labels'
    }

    mat A=J(5,1,.)

    mat rownames A = `rows'

    mat list A

    In fact, I do almost get what I want. However, the problem is that each single word of a multi-word label is treated as a single row . This is why I get:

    Make .
    and .
    Model .
    Mileage .
    (mpg) .

    Instead, the result should look more like:


    Make and Model .
    Milage (mpg) .
    ... .
    ... .
    ... .

    Unfortunately, my attempts of tying labels togther using e.g. "string()" or compound double quotes have been unsuccesful.

    Therefore, I am grateful for any piece of advice on how to solve this seemingly simple problem.
    Last edited by Justus Weinberg; 17 Aug 2017, 03:47.

  • #2
    You have several problems here beyond that stated. One is that matrix row and column names are more limited than variable labels, in their length and in what characters can be included (which certainly don't include spaces). See the help for matrix. Another is that in this example you have a matrix with 5 rows but you have only two variables that qualify.

    You can make some progress like this, but there is no guarantee that the names will look good.

    Code:
    sysuse auto, clear
    
    local rows 
    foreach var of varlist m* {
    local this = strtoname("`: variable label `var''") 
    local rows `rows' `this'
    }
    
    mat A=J(5,1,.)
    
    mat rownames A = `rows'
    
    mac li 
    mat li A

    Comment

    Working...
    X