Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: creating a numeric matrix from string variables


From   joe j <joe.stata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: creating a numeric matrix from string variables
Date   Thu, 4 Jun 2009 13:30:21 +0200

Hi Bill,

Thank you so much for your help. Your explanations were indeed great.
I tried your code on an actual data set of over 3000 firms with up to
7 partners and it worked perfect (with a minor addition of "forvalues"
to generate these many variables in the final matrix, as shown below).
However, there is one thing. My idea, as you guessed correctly, is to
store this matrix as a Stata data set, and I want to merge other
firm-level variables with this matrix, using the company code
(ultimately I'll use some social networking programs to derive certain
measures from this matrix, and these measures will be imported back
and analyzed in Stata along with other firm-level variables). This
means that, I need to:

1. Generate a string variable, let's call it Company, that contains
the company code, as in the file 'mapping' in your code (you named
this variable Partner) . This variable will help me merge other
firm-level variables with the matrix, and

2. I would like the company codes to be the column headings (that's as
the variable names) as well of the matrix, replacing f1, f2, etc in
your code. In a sense these variables are the transpose of the string
variable Company.

I couldn't figure out how to do these, especially 2, as 1 can be done
also by copy/paste. Any suggestions would be appreciated.

Thank you,

Joe

On Wed, Jun 3, 2009 at 11:59 PM, William Gould, StataCorp LP
<wgould@stata.com> wrote:
> Joe J <joe.stata@gmail.com> asked,
>

> I could do this entire problem using only Mata, but that would just be more
> work that is necessary.  Mata really shows its power when used with Stata,
> and vice-versa.  So I tooks Joe's original data and formed form it:
>
>        . list
>
>        . mata: M = agmat(5, "p1 p2 p3")
>        . drop _all
set obs 3388
forvalues j = 1/3388 {
generate f`j'=0
}>        . mata:
>        : st_view(V=., ., .)
>        : V[.,.] = M
>        : end
>
> The result of which is,
>
>        . list
>
>             +------------------------+
>             | f1   f2   f3   f4   f5 |
>             |------------------------|
>          1. |  0    0    2    1    0 |
>          2. |  0    0    1    0    1 |
>          3. |  2    1    0    1    1 |
>          4. |  1    0    1    0    0 |
>          5. |  0    1    1    0    0 |
>             +------------------------+
>
> -- Bill
> wgould@stata.com
> *
> *   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/
>

*
*   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/



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