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

st: Creating a matrix from an excel sheet with data AND row and column names

From   Kerem Yaman <>
Subject   st: Creating a matrix from an excel sheet with data AND row and column names
Date   Thu, 28 Apr 2005 16:58:25 -0700 (PDT)

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

*   For searches and help try:

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