# Re: st: need mata or perhaps egen help

 From "David Merriman" To statalist@hsphsun2.harvard.edu Subject Re: st: need mata or perhaps egen help Date Thu, 22 May 2008 16:46:51 -0500

```Thanks.
Actually, I just did not know this was an option.  This seems even
simpler than Scott's elegant little mata program.  I really appreciate
the help and will try it this way.

> Dear David,
>
> is there any reason you don't want to multiply matrices in Stata?
>
> matrix R=DT*EDI
>
> Best regards,
>
>
>
> On 5/22/08, Scott Merryman <scott.merryman@gmail.com> wrote:
>> You can use -st_data()- and -st_store()- to move data between Stata
>> and mata (see -help m4_stata- )
>>
>> Below is one way:
>>
>> clear
>> //nearest neighbor matrix
>> set obs 20
>> set seed 12345
>> forv i = 1/20 {
>>        qui gen v`i' = uniform()<.2 in  `i'/l
>>        qui replace v`i' = 0 in `i'
>> }
>> mata: dist = makesymmetric(st_data(.,.))
>> mata: dist
>> drop v*
>>
>> //spending
>> set obs 20
>> gen spending = uniform()
>> gen total =.
>>
>> //total spending of neighbors
>> mata
>> spend = st_data(.,"spending")
>> spend
>> X = dist*spend
>> st_store(.,"total", X)
>> end
>> l
>>
>>
>> Scott
>>
>> On Thu, May 22, 2008 at 3:06 PM, David Merriman <dmerrim@gmail.com> wrote:
>> > I have a set of 584 communities and a square (584x584) matrix D that
>> > measures the
>> > distance from each community to each community.
>> >
>> > For each community I also have (584x1) vector (EDI) containing a
>> > variable that measures (essentially) the total amount spent on
>> > economic development incentives.
>> >
>> > I have this data for a number of years but I do not think that is
>> > important to my programming issues.
>> >
>> > For each of my communities I want a single variable that measures the
>> > total amount spent within a given distance (say 15 miles) on economic
>> > development incentives.
>> >
>> > I can convert my D matrix into a zero/1 matrix
>> > (assign zero to home community and and any community more than 15
>> > miles distant, 1 otherwise) and Call  this DT
>> >
>> > Seems I should be then be able to use mata to do the matrix
>> > multiplication
>> >
>> > R=DT*EDI
>> >
>> > R should be a 584x1 vector with the variable I need.
>> >
>> >
>> > The problem (I think) is that I have never used mata and am having a
>> > hard time figuring out how to feed my stata data into mata, do the
>> > multiplication and get the result back  to stata.
>> >
>> > Can anyone tell me how to do this?
>> > Maybe there is an easier way to do it with egen or some other stata
>> > commands.
>> >
>> > Thanks
>> > Dave Merriman
>> >
>> *
>> *   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/
>

--
David Merriman
dmerrim@gmail.com
*
*   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/
```