Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: How to change matrix row names

From   Friedrich Huebler <>
Subject   st: How to change matrix row names
Date   Mon, 1 Nov 2004 09:55:45 -0800 (PST)

This is a followup to a question I posted on October 13. I found one
solution with the help of Stata tech support but hope for additional
advice from Statalist members. To make the problem clearer I include
more information than in my original message.

I am looking for a way to save a matrix that was created with
-tabstat, by()-. An important assumption is that the value labels of
the -by- variable are not known in advance. I want to save the row
names together with the matrix values but don't know what to do when
the row names contain certain special characters like spaces or

Think of a national dataset with a variable "state"; we know the name
of the variable and the name of the value label (e.g., "statename")
but we don't know the individual labels (e.g., "New Jersey", "New
Mexico" and so on). This case can be reproduced with the auto data.
The following commands create a 3x1 matrix with row names that
contain spaces. The matrix is then saved in two new variables called
"table1" and "label". I use -tabstatmat- from SSC (-findit
tabstatmat-) and -svmat2- from STB-56 (-findit svmat2-).

. sysuse auto, clear
. lab def origin 0 "Domestic cars" 1 "Foreign cars", modify
. tabstat mpg, by(foreign) save
. tabstatmat table
. svmat2 table, rnames(label)

I use -svmat2- because I don't know any other command that places
matrix row names in a variable. The matrix has three rows with the
names "Domestic cars", "Foreign cars", and "Total". -svmat2-
interprets ever single word as a label so that the saved matrix looks
as follows.

. clist label table1 if table1<., noobs

   label     table1
Domestic   19.82692
    cars   24.77273
 Foreign    21.2973

My theoretical solution was to change the labels "Domestic cars" and
"Foreign cars" to "Domestic_cars" and "Foreign_cars". I tried
replacing all spaces with underscores but the following sequence of
commands changes all row names to "Domestic_cars_Foreign_cars_Total".

. local names: rowfullnames table
. local names: subinstr local names " " "_", all
. matrix rownames table = `names'

Can one of the matrix wizards on this list suggest a way to replace
the spaces in the row names? In the end I hope to create two
variables that look like this:

        label     table1
Domestic_cars   19.82692
 Foreign_cars   24.77273
        Total    21.2973

Stata tech support provided one solution in which individual value
labels are modified before the matrix is created. This solution
requires saving the labels in a temporary file on the hard drive.
During my exchange with tech support I was told that there are
numerous ways to modify label names and very few that modify matrix
row and column names. Therefore it was recommended to tackle the
problem upstream. Nevertheless, I think that manipulating the
-tabstat- matrix, if possible, would be more efficient because it
should not require hard drive access.

Thank you,

Friedrich Huebler

Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
*   For searches and help try:

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