Announcement

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

  • Storing results in a matrix

    Hi Stata users,

    I would like to store the results of cross-tabulations in a matrix. I have one variable that am cross-tabbing with other variables and would want the results for the first tabulation to be stored in the first and second columns of the matrix, results for the second tabulation to be stored in the third and fourth columns of the matrix

    The data example is
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 var1 str4(var2 var3)
    "yes" "low"  "high"
    "no"  "high" "low" 
    "yes" "low"  "high"
    "yes" "high" "low" 
    "yes" "high" "low" 
    "yes" "high" "high"
    "no"  "high" "high"
    "yes" "low"  "low" 
    "no"  "low"  "high"
    "no"  "high" "low" 
    "yes" "high" "high"
    "no"  "low"  "low" 
    "yes" "low"  "high"
    "no"  "high" "low" 
    "yes" "low"  "high"
    "no"  "high" "high"
    "yes" "low"  "high"
    "yes" "high" "low" 
    "yes" "low"  "low" 
    "yes" "low"  "low" 
    "yes" "high" "low" 
    "yes" "low"  "low" 
    end

    The code am using is
    Code:
        matrix P = J(2,4,.)
        
        local i = 1
        
        foreach v of varlist var2 var3 {
            ta var1 `v', matcell(x)
            mata: st_matrix("xx", (st_matrix("x") * 100) :/ rowsum(st_matrix("x")))
            matrix list xx
            matrix P[1,`i'] = xx
            local i = `i' + 1
        }
        
        matrix list P
    Thanks in advance!

  • #2
    I have found a tiny error in my code

    Code:
    local i = `i' + 1
    should be
    Code:
    local i = `i' + 2

    Comment

    Working...
    X