Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: How to insert the name of a variable into a matrix automatically

From   yaacov lawrence <>
Subject   st: How to insert the name of a variable into a matrix automatically
Date   Sun, 9 Sep 2012 18:58:37 +0300

For a medical epidemiological project looking at cancer incidence I
should like Stata to create a descriptive table of the variables as a
matrix that I will then export to Excel. There will be one row for
each variable. The various columns include number of observations,
median age, etc. I am using the "foreach" command.

All works well.
The only problem is that I should like the first cell of each row to
contain the variable name, and I cannot see how to do this. I realize
that I could do this manually using "matrix rownames" but I would
rather do it automatically.

example of the code below:

thank you!!


matrix cancertypes = J(10,4,.)
matrix colnames cancertypes = "cancer_type" N median_age "%white"
local rownumber 1

foreach var of varlist head lowgi {
count if `var'==1
matrix cancertypes[`rownumber',2]=r(N)
summarize AGE if `var'==1, detail
matrix cancertypes[`rownumber',3]=r(p50)
local rownumber=`rownumber'+1

matrix list cancertypes


Dr Yaacov Richard Lawrence MBBS MA MRCP
Sheba Medical Center
Tel HaShomer 52621
*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index