Re: st: Making a Matrix from Three Variables

 From Amadou DIALLO To statalist@hsphsun2.harvard.edu Subject Re: st: Making a Matrix from Three Variables Date Sat, 9 May 2009 21:13:21 +0300

```Hi Allan,

This is my 3 cents solution (have not get the time to put it in a
formal mata programm, just tested on the command line). I hope it can
lead you to what you need to achieve if you play around a bit with it.

Best regards.

clear

// suppose you have 3 variables x, y, z, with number of observations
of x+y = number of observations of z

set obs 10

g x=.
replace x=1 in 1
replace x=2 in 2

g y=.
replace y =1 in 1
replace y =2 in 2
replace y =3 in 3
replace y =4 in 4
replace y =5 in 5

//g z = round(uniform(),1)

g z=.
replace z =1 in 1
replace z =2 in 2
replace z =3 in 3
replace z =4 in 4
replace z =5 in 5
replace z =6 in 6
replace z =7 in 7
replace z =8 in 8
replace z =9 in 9
replace z =10 in 10

// tomata  x y z, missing // Don't know why not working
tomata x in 1/2
tomata y in 1/5
tomata z

mata:

c = rows(x)
d = cols(y')
M=J(c,d,.)
for(i=1;i<=d;i++) {
M[1,i] = z[i]
k=d+i
M[c,i] = z[k]
}
M
z
end

2009/5/9, Glenn Goldsmith <glenn.goldsmith@gmail.com>:
> Hi Allan,
>
> It seems like what you're after is more-or-less the same as converting from
> sparse matrix storage, so the mata code here should do the trick:
>
> http://www.stata.com/statalist/archive/2009-04/msg00142.html
>
> You just need to get your variables into a mata matrix to start with and you
> should be away:
>
>   mata : st_view(x=.,.,("var2","var1","var3"))
>
> NB: Note the inverted order of var1 and var2. This is because the code for
> -sparse()- assumes that the first variable provides the row index, and the
> second variable provides the column index.
>
> HTH,
>
> Glenn.
>
> Allan Joseph Medwick <amedwick@gmail.com> wrote:
>
> Hi, All,
>
> I have three variables (var1, var2, var3).  I would like to create a
> matrix where the values of var1 are the columns (ascending), the
> values of var2 are the rows (also ascending), and the values of var3
> are the elements in the matrix.  I know there must be a user defined
> procedure out there to do this, but I haven't been able to find it.
>
> Thanks,
> Allan
>
>
> *
> *   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/
>

--
---

Development Economist
Director, Center for Research and Training on Adult Education
Mayotte, FRANCE
www.aprosasoma.org
+262639693250
*
*   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/
```