Announcement

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

  • Conformability error r(503) when naming matrix columns and rows

    Hi all,
    I am attempting to create a series of matrices with row and column names, with the ultimate goal of exporting these matrices to excel. I am using Stata 15.1 on Windows 7. I used this post as a guide, but am encountering issues when it comes to applying names to the matrix. https://www.statalist.org/forums/for...d-column-names. An example of my process is below:


    Code:
    clear
    set more off
    
    *----- example data -----
    
    webuse citytemp2
    
    mdesc // note no missing data for the two variables
    drop if missing(division)
    drop if missing( agecat )
    
    tab division agecat, missing matcell (freq)
    
    matrix list freq
    
    matsum freq, column(columntotal)
    
    //create matrix with each cell as a percent of the columntotal
    mata : st_matrix("percent", st_matrix("freq") :/st_matrix("columntotal"))
    
    matrix list percent
    
    *----- levels only -----
    
    levelsof division, missing local(down)
    levelsof agecat, missing local(across)
    
    matrix rownames percent = `down'
    matrix colnames percent = `across'
    
    *----- labels too -----
    
    foreach var of varlist division agecat {
        
        // retrieve levels of each variable
        levelsof `var', local(`var'_levels)
        
        // create local with all corresponding value labels
        foreach val of local `var'_levels {
            local `var'vl ``var'vl' `"`: label (`var') `val''"'
        }
    
    }
    
    //apply value labels as matrix row and column names
    matrix rownames percent = `divisionvl'
    matrix colnames percent = `agecatvl'
    
    matrix list percent


    The problem is that I receive the error message " matrix rownames percent = `divisionvl' conformability error r(503); " From reading the previous Stata list post that I reference above, I think I could understand how this error might arise if there were missing values for the data I wanted to use. However, that's not the case here- neither variable in this example has any missing values.Does anyone have any insight on how I can avoid this error in this example, and potentially with data that does contain missing values? Thanks!

  • #2
    Your problem is in this nested loop.
    Code:
    foreach var of varlist division agecat {
        
        // retrieve levels of each variable
        levelsof `var', local(`var'_levels)
        
        // create local with all corresponding value labels
        foreach val of local `var'_levels {
            local `var'vl ``var'vl' `"`: label (`var') `val''"'
        }
    
    }
    I ran the following after the end of these loops.
    Code:
    . display `"`divisionvl'"'
    N. Eng. `"Mid Atl"' `"E.N.C."' `"W.N.C."' `"S. Atl."' `"E.S.C."' `"W.S.C."' `"Mountain"' `"Pacif
    > ic"'
    
    . display `"`agecatvl'"'
    19-29 `"30-34"' `"35+"'
    You see the problem. The explanation is complicated, and I can't locate at the moment the recent thread in which this was discussed. In any event, swapping in
    Code:
         local `var'vl `"``var'vl' "`: label (`var') `val''""'
    for the similar line in your code results in
    Code:
    . display `"`divisionvl'"'
     "N. Eng." "Mid Atl" "E.N.C." "W.N.C." "S. Atl." "E.S.C." "W.S.C." "Mountain" "Pacific"
    
    . display `"`agecatvl'"'
     "19-29" "30-34" "35+"
    
    . //apply value labels as matrix row and column names
    . matrix rownames percent = `divisionvl'
    
    . matrix colnames percent = `agecatvl'
    
    . 
    . matrix list percent
    
    percent[9,3]
                  19-29      30-34        35+
     N. Eng.   .0433925  .13607595  .03007519
     Mid Atl  .04733728  .12658228   .2481203
      E.N.C.   .2209073  .25949367  .09022556
      W.N.C.  .09861933  .03164557  .13533835
     S. Atl.  .11439842  .05063291  .30827068
         Se.  .06508876  .04113924          0
      W.S.C.  .09467456  .12341772  .01503759
    Mountain  .11439842  .00949367          0
     Pacific  .20118343  .22151899  .17293233

    Comment

    Working...
    X