Announcement

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

  • Running into the "invalid name" error when using value labels as matrix rownames with putexcel

    I am using Windows 7, 64-bit, and Stata 14.2 and I am aiming to send crosstabulation of one string variable and value labels of its numeric version to excel using the following code:

    local root "X/metadata/study1/"
    cap mkdir `root'/mapping
    local putexcel `root'/mapping/crs_bfup.xlsx
    putexcel set `putexcel', sheet(start) replace


    rename hdoc hdoc_t
    gen hdoc=.
    replace hdoc = 1 if hdoc_t == "P"
    replace hdoc = 2 if hdoc_t == "C"
    replace hdoc = 3 if hdoc_t == "M"
    replace hdoc = 4 if hdoc_t == "T"
    replace hdoc = 5 if hdoc_t == "N"
    label define hdoc 1 "Child Health Passport", modify
    label define hdoc 2 "Child Health Card (green print)", modify
    label define hdoc 3 "Missing(P or C is there but temp. not available)", modify
    label define hdoc 4 "Temporary record(eg piece of paper)", modify
    label define hdoc 5 "None (never issued or lost for good)", modify
    label define hdoc 8 "Unknown", modify
    label define hdoc 9 "Missing", modify
    label val hdoc hdoc

    //creating a crosstab of the original string and the new numeric variable and
    //sending the crosstab to excel
    putexcel set `putexcel', sheet(hdoc) modify
    decode hdoc, gen(hdoc_s)
    tabulate hdoc_s hdoc_t, m matcell(cellcounts)
    levelsof hdoc_s, miss local(hdoc_labels)
    levelsof hdoc_t, miss local(hdoc_t_values)
    matrix rownames cellcounts = `hdoc_labels'
    matrix colnames cellcounts = `hdoc_t_values'
    putexcel A1 = matrix(cellcounts), names hcenter

    drop hdoc_t hdoc_s


    it seems to be working fine until get to matrix rownames cellcounts = `hdoc_labels'
    where I get the following error
    None (never issued or lost for good) invalid name

    I basically adapted the code from http://blog.stata.com/2017/01/24/cre...a-expressions/
    Example 8

    I want ot do this for a lot of varibles...

    Your help will be greatly appreciated.

    Thanks!


  • #2
    Matrix rownames are limited to 32 characters (see technical note #4 on page 3 of https://www.stata.com/manuals13/pmatrixrownames.pdf, though this may be documented elsewhere as well). Stata is having trouble with "None (never issued or lost for good)" because it has 36 characters - if you shorten it (and all other value labels), your code should work, I think.

    Comment


    • #3
      many thanks Issac Maddow-Zimet for the response. It seems its not only the "None (never issued or lost for good)" label that is beyond 36 chars, however if I reduce it ("None (never issued or lost for good)") to None and rerun, the code works okay even though it still has labels that are more than 36 chars e.g "Missing(P or C is there but temp. not available)", so probably there is more to it maybe?

      Comment


      • #4
        "Missing(P or C is there but temp. not available)" is being accepted because of the period in the middle of the label - you can see that if you take it out, the code will no longer function. I'm actually not 100% sure why, but I've noticed that Stata behaves a bit strangely when you assign a matrix rownames with periods in it; the character limit only applies to the portion before the period (in this case, 32 characters). My best guess would be that it has to do with factor notation, which also results in rownames with periods in them (e.g. 1b.varname or 2.varname), but I couldn't find good documentation on how this interacts with character limits.

        Comment

        Working...
        X