Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to insert the name of a variable into a matrix automatically
From 
 
Nick Cox <[email protected]> 
To 
 
[email protected] 
Subject 
 
Re: st: How to insert the name of a variable into a matrix automatically 
Date 
 
Mon, 10 Sep 2012 13:36:39 +0100 
Stata won't support the inclusion of textual information in a matrix.
Rather, you mean you want automation of row names, which can be
thought of as marginal labels. This is exactly equivalent to
automating column names.
matrix cancertypes = J(2,2,.)
matrix colnames cancertypes = N median_age
matrix rownames cancertypes = head lowgi
local row = 1
qui foreach var of varlist head lowgi {
                      count if `var'==1
                      matrix cancertypes[`row',2]=r(N)
                      summarize AGE if `var'==1, detail
                      matrix cancertypes[`row',2]=r(p50)
                       local row = `row'+1
 }
matrix list cancertypes
Nick
On Sun, Sep 9, 2012 at 4:58 PM, yaacov lawrence <[email protected]> wrote:
> 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!!
>
> yaacov
> -------------
>
> 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
>
*
*   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/