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

Re: st: Allocating values from a matrix to a new variable in Stata

From   Nick Winter <>
Subject   Re: st: Allocating values from a matrix to a new variable in Stata
Date   Tue, 03 May 2005 13:48:55 -0400

Assuming you have the distance matrix in a Stata matrix called "D", then this should do it:

. generate distance = D[x,y]

(I am assuming that the diagonal of the distance matrix contains zeros, so there is no need to deal with x==y as a separate step.

If you have missing values for either x and/or y, you might need to modify:

. generate distance = D[x,y] if !missing(x) & !missing(y)

--Nick WInter

At 01:41 PM 5/3/2005 -0400, you wrote:


I'm using Intercooled Stata 8, and am not an experienced user.
It will be great if someone can help me out with this.

I have a dataset where I have about 18,000 records each having a value for X
and another for Y that ranges from 1 to 32.
I have another 32*32 matrix that gives the distances between all X and Y

Whenever a record has X=Y, I want to introduce a new variable distance =
Otherwise, I want to assign distance = the appropriate distance value for
(X,Y) from this matrix I have.

For example, if X=1 and Y=1, distance = 0.
If X=1 and Y=2, distance = 25 (value obtained from the distance matrix)
If X=2 and Y=1, distance = 25 (same as above)
If X =1 and Y=3, distance = 15 ...... And so on.

Can someone please suggest the most efficient way to do this in Stata?

Many thanks,

Anjali Mahendra
Doctoral student
Department of Urban Studies and Planning
Massachusetts Institute of Technology

*   For searches and help try:
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government e
308 White Hall w
Cornell University
Ithaca, NY 14853-4601

* For searches and help try:

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