Ben Jann
> > I've got two related questions regarding column and row names.
> > (1) How can I replace the 'jth' column name only?
> > (2) How can I save as a macro the 1st j column names from
> a matrix?
>
> As far as I know, there are no specific functions for such
> purposes and
> you have to use some work arounds.
>
> (1) To change the name of row 2 to "whatever":
>
> mat a=(1 \ 2 \ 3)
> forv i=1/`=rowsof(a)' {
> if `i'==2 local rnames "`rnames' whatever"
> else local rnames "`rnames' `:word `i' of `:rownames(a)''"
> }
> mat rownames a=`rnames'
> mat list a
>
> (2) To save the first 2 rownames as a macro:
>
> mat a=(1,2\ 3,4\ 5,6)
> forv i=1/2 {
> local rnames "`rnames' `:word `i' of `:rownames(a)''"
> }
> di "`rnames'"
>
> Note that the rownames *must not* contain spaces, since the extended
> macro function -rownames()- returns the names without quotes.
> Furthermore, only names will be changed but not eqnames (use the
> extended macro function -fullrownames()- if you want to
> change both).
>
> The resulting macro 'rnames' will have a leading space
> (usually, this is
> irrelevant). If you want to remove it, type
>
> local rnames: list retokenize rnames
>
My analysis resembles Ben's.
To develop a key point, you can assign
a row or column name including one or
more spaces, but you cannot retrieve
it (easily).
. mat A = J(2,2,1)
. mat rownames A = "two words" "three more words"
. mat li A
symmetric A[2,2]
c1 c2
two words 1
three more words 1 1
However, in reverse, the words of `: rownames A'
are, when -tokenize-d,
_1: two
_2: words
_3: three
_4: more
_5: words
Not long ago, I suggested to Stata Corp
adding a handle to pick up such row and column names.
Nick
[email protected]
P.S. on Ben's last point, an alternative is
local rnames "`rnames'`:word `i' of `:rownames(a)'' "
leaving a trailing space. This is a sense the same
problem, except that a trailing space is often
not so obtrusive as a leading one.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/