Announcement

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

  • Matrix colnames and/or rownames from matrix or from value labels

    Dear all,

    I have successfully generated a matrix after a tabulate command, but unsuccessfully set the right column and row names and would love a bit of help.

    I found online the following strategy to name columns and rows in a matrix:
    Code:
        tab age Grade if Enrolled==1 & inrange(age,5,35) [iw=wt_hh], matcell(T1)
        decode age, gen(age_s)
        levelsof age_s, local(agelabels)
        matrownames T1 = `agelabels'
        decode Grade, generate(Grade_s)
        levelsof Grade_s, local(Gradelabels)
        mat colnames T1 = `Gradelabels'
    However, I have two problems with this: (1) my age variable is not labeled, and (2) decode results in an alphabetical order that does not match the results matrix order. While the matrix has the following column names after tabulating:
    Code:
    "Primary 1" "Primary 2" "Primary 3" "Primary 4" "Primary 5" "Primary 6" "JSS 1" "JSS 2" "JSS 3" "SSS 1" "SSS 2" "SSS 3" "SSS 4"
    Using the code above leads to the following column names, which means the resulting matrix would show "JSS 1" when it should be saying "Primary 1":
    Code:
    "JSS 1" "JSS 2" "JSS 3" "Primary 1" "Primary 2" "Primary 3" "Primary 4" "Primary 5" "Primary 6" "SSS 1" "SSS 2" "SSS 3" "SSS 4"'
    To solve problem (1) I would have liked to set the rownames from a matrix already generated, something like the following:
    Code:
        tab age Grade if Enrolled==1 & inrange(age,5,35) [iw=wt_hh], matcell(T1) matrow(rows)
        mat T1 = T1
        mat rownames T1 = `rows'
    But I failed, so simply did the following, which works well enough for my purposes:
    Code:
        tab age Grade if Enrolled==1 & inrange(age,5,35) [iw=wt_hh], matcell(T1) matrow(rows)
        mat T1 = rows, T1
    However, I have not been able to fix problem (2). I have played with defining the local myself, but I also failed at that:
    Code:
    local labels ""
            levelsof Grade, local(dvs)
            foreach level in `dvs' {
                local varlabelname: value label Grade
                local varName: label `varlabelname' `level'
                local labels "`labels'" " " "`varName'"
                }
            di "`labels'"
    Any ideas on how to do this without having to manually add the column names?

    I am using Stata 13 by the way.

    Thank you all very much!
    Last edited by Mariana P-C; 05 May 2017, 12:21.

  • #2
    I have a similar problem. Any help would be really nice. I am using Stata 17. After decoding the column and row variable names, the contents are giving incorrect frequencies.

    Comment

    Working...
    X