Here is one way: using -levels- to obtain the names and -matname- to rename
the rows.
. insheet using "C:\Documents and Settings\Scott_2\Desktop\foo.txt"
(4 vars, 3 obs)
. mkmat var*, matrix(A)
. matrix list A
A[3,3]
var1 var2 var3
r1 1 1 2
r2 2 2 1
r3 0 0 2
. levels namesvar , local(names)
`"coffee"' `"cotton"' `"sugar"'
. matname A `names', rows(.) explicit
. matrix list A
A[3,3]
var1 var2 var3
coffee 1 1 2
cotton 2 2 1
sugar 0 0 2
Hope this helps,
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of Kerem Yaman
> Sent: Thursday, April 28, 2005 6:58 PM
> To: [email protected]
> Subject: st: Creating a matrix from an excel sheet with data AND row and
> column names
>
> I want to use stata to dynamically read historical
> information from Excel on an expandable list of
> things and also a matrix that codifies some
> information on how these things are interrelated
> which I will later interpret in a do-file.
>
> So e.g., I would try to get stata to build a matrix
> for me by reading an excel sheet that has data in
> the format of:
>
> namesvar var1 var2 var3
> cotton 1 1 2
> sugar 2 2 1
> coffee 0 0 2
>
> This data is not of a fixed size but I am happy to
> keep it always of the same form where namesvar lists
> the names of the variables (same for columns and
> rows) and the data is ordered in a meaningful way so
> if I add another var like "wheat" to the above then
> I add a column of 4 numbers and a row with "wheat"
> and also 4 numbers.
>
> I was going to read the sheet with:
> insheet using "TestNames.csv"
>
> and then do:
>
> mkmat var*, matrix(X)
>
> I can get the data (the numbers) into the matrix.
>
> My question is: How do I get the namesvar into a
> "list" so I can use it to rename the rows and
> columns? I have programming experience in other
> languages but cannot figure out how to do this in
> stata (beginner in stata programming)
>
> I tried this as follows in a do-file:
>
> local nlist = ""
>
> local tempstr = ""
>
> forval i = 1(1)3 { *really 3 supposed to be _N but
> trying to keep it simple for debug
>
> list namesvar *debug
>
> disp namesvar[`i'] *debug
>
> local tempstr = namesvar[`i']
>
> local nlist = `nlist' + "`tempstr'"
>
> disp `nlist' *debug
>
> }
>
> But even though the names display correctly, they do
> not work in the "string list builder" line.
> One more issue I may have is that the number of
> variables can potentially be rather large like 100 or
> so. From what I can see there are limitations to the
> length of strings in Stata. So maybe even if I can
> solve the above problem I am still stuck with this
> problem??
>
> *
> * 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/
*
* 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/