Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: Stata equivalent to st_matrixcolstripe?


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: RE: Stata equivalent to st_matrixcolstripe?
Date   Thu, 16 Apr 2009 15:07:01 -0400

Thomas Jacobs <[email protected]>:

The macro extended functions are much less useful if you have spaces
or other special characters in the names, as shown by this example:

mat b=J(2,3,1)
mat rownames b="row x: item 1" "row y: item 2"
mat colnames b="eq1: item 1" "eq1: item 2" "eq.2: item 3"
mat li b
loc c: colnames b
mat colnames b=`c'

*Sometimes you'd like to be able to do something like:

mat c=J(2,3,0)
matnames b
mat colnames c=`r(c)'
mat rownames c=`r(r)'
mat li c

Since I often use spaces or other special characters in the names
attached to matrix rows and columns, I have a little program that gets
properly quoted names and puts them in returned results, so you can
run the above example (if you save the below code to
/ado/matnames.ado):

*! Program to put row and column names in r()
*! posted to Statalist by Austin Nichols 16 April 2009
prog matnames, rclass
 version 9.2
 syntax anything [, *]
 cap conf matrix `anything'
 if _rc {
  di as err "matrix `anything' not found"
  error 198
  }
 forv i=1/`=rowsof(`anything')' {
  mata: getRNames("`anything'",`i')
  loc r `"`r' `"`r1':`r2'"'"'
  }
 forv i=1/`=colsof(`anything')' {
  mata: getCNames("`anything'",`i')
  loc c `"`c' `"`r1':`r2'"'"'
  }
 return local r=`"`r'"'
 return local c=`"`c'"'
end
version 9.2
mata:
 void getRNames(string scalar b, real scalar i)
 {
   r=st_matrixrowstripe(b)
   st_local("r1", r[i,1])
   st_local("r2", r[i,2])
 }
 void getCNames(string scalar b, real scalar i)
 {
   c=st_matrixcolstripe(b)
   st_local("r1", c[i,1])
   st_local("r2", c[i,2])
 }
end


One could also construct both local macros in one call to Mata, and
probably there are other improvements possible, but the above works
for what I need, and perhaps it will suit your needs as well.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index