I have a long list of observations from many countries. Each person was asked to rate six items on a 0-10 scale. I would like to compare the rankings of these items across countries, where the ranking is determined by the average ratings. I will also compare each country to an overall "world" ranking, determined the same way.
The world rankings were easy to do, and I used Mata in just one line, to sort a matrix I created:
Now I would like to do the same thing for each country, creating variables rankA1c-rankA6c that contain the rankings at the country level. The first part of the code is the same. Where I'm having trouble is the Mata line. st_matrix() brings the matrix into Mata, and I know st_local() can bring a local macro into Mata. The problem lies in combining them. I need to refer to a matrix using a local macro, since there will be a different matrix for each country. This is one version I have tried:
This gives the error "unexpected end of line, <istmt> incomplete." I also tried replacing the mata line with the previously working line in the code at the top (without the "lvl" macro). In this case, the code runs, but the mata line appears to be ignored. Running it with trace on, it looks like Stata is ignoring it entirely. Here is a short snippet of the results:
My question is, how can I run this line of code within the loop, referring to a matrix using a local macro? And why is Stata apparently ignoring the Mata line within the loop?
The world rankings were easy to do, and I used Mata in just one line, to sort a matrix I created:
Code:
// collect the overall averages foreach i of numlist 1/6 { qui sum itemA`i' local a`i' = r(mean) } // put into a matrix with item# in first col and rating in second matrix ratingsworld = J(6,2,.) foreach i of numlist 1/6 { matrix ratingsworld[`i',1] = `i' matrix ratingsworld[`i',2] = `a`i'' } matrix list ratingsworld // sort according to item rating. note that the highest ranked is now in sixth row. mata : st_matrix("ratingsworld", sort(st_matrix("ratingsworld"), 2)) matrix list ratingsworld // gen new vars showing the world rank of each item foreach i of numlist 1/6 { local v = ratingsworld[6-`i'+1,1] gen rankA`v'w = `i' }
Code:
levelsof cntry, local(levels) foreach lvl of local levels { // avg by cntry foreach i of numlist 1/6 { qui sum itemA`i' if cntry==`lvl' local a`i' = r(mean) } // into item-rating matrix matrix ratings`lvl' = J(6,2,.) foreach i of numlist 1/6 { matrix ratings`lvl'[`i',1] = `i' matrix ratings`lvl'[`i',2] = `a`i'' } matrix list ratings`lvl' // sort mata : st_matrix("ratingsworld"+st_local("lvl"), sort(st_matrix("ratingsworld"+st_local("lvl"),2)) matrix list ratings `lvl' }
Code:
---------------------------------------------------------------------------------------------------- end _matrix_cmds.List --- - exit ----------------------------------------------------------------------------------------------------------- end _matrix_cmds --- - mata : st_matrix("ratingsworld", sort(st_matrix("ratingsworld"), 2)) - matrix list ratings`lvl' = matrix list ratings24 --------------------------------------------------------------------------------------------------------- begin _matrix_cmds ---
Comment