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: variable names in matrix
From
daniel klein <[email protected]>
To
[email protected]
Subject
Re: st: variable names in matrix
Date
Tue, 10 May 2011 00:53:27 +0200
Stefan,
there are several problems with your code. Firstly,
"mat A=J(1,2,.)"
will not work, because in your example your matrix A has 3 rows, not
only 1. Secondly you do not need 2 colums, because, as you have
already pointed out, a matrix may not conatin strings. But see -help
mat rownames- for an alternative.
"scalar aaa=sum(`var')
mat A=(A \ ??? , aaa)"
will also not do what you probably expect it to do. Firstly -sum()-
returns the runnig sum of a variable. You are probably looking for
-total()-, an -egen- function. Secondly, within a loop you want to
code something like
. mat A[`i', 1] = x
Please see -help matrix-.
Here are two rather ad hoc solutions:
/*---example 1---*/
tempvar s
unab vars : *
mat A = J(`: word count `vars'', 1, .)
mat rownames A = `vars'
loc i 0
foreach v in `vars' {
qui g `s' = sum(`v')
mat A[`++i', 1] = `s'[_N]
drop `s'
}
matlist A
/*---end---*/
/*---example 2---*/
mata :
A = .
st_view(A, ., .)
st_matrix("A", colsum(A)')
end
unab nams : *
mat rownames A = `nams'
matlist A
/*---end---*/
*
* 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/