Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: calculating many distances and storing them in many new variables

 From "Michael Ralph M. Abrigo" To statalist@hsphsun2.harvard.edu Subject Re: st: calculating many distances and storing them in many new variables Date Fri, 21 Oct 2011 18:44:19 +0800

```Hi, Ana,
A did something similar recently. You can do something like  :

*-------------
tempvar hospid
encode hosp_ch, gen(`hospid')
qui su `hospid'
local maxid = r(max)
forval x = 1/`maxid' {
qui su lat_hosp if `hospid' == `x'
local lat = r(mean)
qui su lon_hosp if `hospid' == `x'
local lon = r(mean)
gen dist_hosp`x' = sqrt((lat_pat - `lat')^2 + (lon_pat - `lon')^2)
// or use another distance formula
label var dist_hosp`x' "distance from hospital `x'"
}
*-------------

or if ids are really in a b c d

*-------------
foreach x in a b c d {
qui su lat_hosp if hosp_ch == "`x'"
local lat = r(mean)
qui su lon_hosp if hosp_ch == "`x'"
local lon = r(mean)
gen dist_hosp`x' = sqrt((lat_pat - `lat')^2 + (lon_pat - `lon')^2)
label var dist_hosp`x' "distance from hospital `x'"
}
*-------------

HTH
Michael
On Fri, Oct 21, 2011 at 6:16 PM, Vitorino, Maria Ana
<vitorino@wharton.upenn.edu> wrote:
>
> Dear Yuval,
> Thanks. But the goal  is different. I know to which hospital each patient went. What I'm trying to do is to calculate the distance between each patient and each hospital alternative (ie all the hospitals that the patient could have decided to go to).
> I'm guessing this can be done with some sort of loop...
>
> Ana
>
>
> On Oct 21, 2011, at 4:42 AM, "Yuval Arbel" <yuval.arbel@gmail.com> wrote:
>
> > Anna,
> >
> > I'm not sure what your precise goal is. But if your target is only to
> > map what hospital each patient went, may I suggest the following:
> >
> > Suppose you have a column of coordinates under "patient" and another
> > column under "hospitals"
> > If you are working with STATA11 you can try running:
> >
> > reg patient i.hospitals
> >
> > Let me show you what is the output you get by using one of the
> > examples I ran: I collected a small dataset, which measures land
> > values (rent) and distance from Tel Aviv center (distance):
> >
> > . reg rent i.distance
> >
> >      Source |       SS       df       MS              Number of obs =      55
> > -------------+------------------------------           F(  9,    45) =   12.43
> >       Model |   294279904     9  32697767.1           Prob > F      =  0.0000
> >    Residual |   118368183    45  2630404.07           R-squared     =  0.7131
> > -------------+------------------------------           Adj R-squared =  0.6558
> >       Total |   412648087    54  7641631.24           Root MSE      =  1621.9
> >
> > ------------------------------------------------------------------------------
> >        rent |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
> > -------------+----------------------------------------------------------------
> >    distance |
> >       1000  |  -8245.833    1238.71    -6.66   0.000    -10740.72   -5750.943
> >       1500  |  -8087.026   1038.817    -7.78   0.000    -10179.31   -5994.741
> >       2000  |  -6353.833   1067.634    -5.95   0.000    -8504.158   -4203.509
> >       2500  |  -7560.333   1480.542    -5.11   0.000     -10542.3    -4578.37
> >       3000  |  -6545.958   1097.999    -5.96   0.000    -8757.442   -4334.475
> >       3500  |      -9500   1324.237    -7.17   0.000    -12167.15    -6832.85
> >       4000  |  -9891.905   1119.184    -8.84   0.000    -12146.06   -7637.752
> >       4500  |  -10258.33    1238.71    -8.28   0.000    -12753.22   -7763.443
> >       5000  |  -11600.33   1872.753    -6.19   0.000    -15372.25   -7828.414
> >             |
> >       _cons |   13433.33   936.3767    14.35   0.000     11547.37    15319.29
> > ------------------------------------------------------------------------------
> >
> > Note that the distance variable includes values from 500 to 5000
> > meters. The command made the 500 meter distance the base category.
> >
> > On Fri, Oct 21, 2011 at 5:13 AM, Maria Ana Vitorino
> > <vitorino@wharton.upenn.edu> wrote:
> >> Dear StataList users,
> >>
> >> Suppose we have the following toy data which has a list with patients, the
> >> hospital to which they went (hosp_ch) and the coordinates for both.
> >>
> >> patient_id hosp_ch      lat_pat lng_pat lat_hosp        lng_hosp
> >> 1               a               2               4               5
> >>     6
> >> 2               a               1               3               5
> >>     6
> >> 8               c               3               5               10
> >>    12
> >> 9               a               5               2               5
> >>     6
> >> 12              b               8               6               8
> >>     9
> >>
> >>
> >> What I would like to do is:
> >> **To create new variables with the distances from every patient to every
> >> possible hospital in the data. So, I would like 3 new columns which will
> >> contain the distance from each patient to each hospital in the data.
> >> **Also, I would like to have those new columns labeled dist_a, dist_b and
> >> dist_c.
> >>
> >> Is there an efficient way to achieve this?
> >> In the real data, I have many more patients and hospitals (hundreds in fact)
> >> so I would like to generate these variables in an as automated way as
> >> possible.
> >>
> >> Any help is appreciated.
> >> Thanks!
> >> Ana
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >> *
> >> *   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/
> >>
> >
> >
> >
> > --
> > Dr. Yuval Arbel
> > 4 Shaar Palmer Street, Haifa, Israel
> > e-mail: yuval.arbel@gmail.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/

--
Michael Ralph M. Abrigo
Research Specialist
Philippine Institute for Development Studies
Tel.: (632) 893-9585 loc. 312
E-mail: mabrigo@pids.gov.ph / mmabrigo@gmail.com

"I am most anxious for liberties for our country... but I place as a
prior condition the education of the people so that our country may
have an individuality of its own and make itself worthy of
liberties... " Jose Rizal,1896

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