Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Maria Ana Vitorino <vitorino@wharton.upenn.edu> |

To |
<statalist@hsphsun2.harvard.edu> |

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

Date |
Sun, 23 Oct 2011 19:24:03 -0400 |

Thanks so much for all your suggestions! They worked great. Ana On Oct 21, 2011, at 8:59 AM, Nick Cox wrote:

That's my guess too in terms of "can", but not at all my guess interms of "should".You're implying that you want _hundreds_ more variables, butalthough that's not insuperable in terms of storage, it still raisesthe question of how you are going to work with those extra variables.Here's an alternative approach:. input patient_id str1 hosp_ch lat_pat lng_pat lat_hosplng_hosppatient~d hosp_ch lat_pat lng_pat lat_hosp lng_hosp1. 1 a 2 45 62. 2 a 1 35 63. 8 c 3 510 124. 9 a 5 25 65. 12 b 8 68 96. end . fillin patient_id hosp_ch . bysort hosp_ch (lat_hosp) : replace lat_hosp = lat_hosp[1] . by hosp_ch : replace lng_hosp = lng_hosp[1] . bysort hosp_ch (lat_pat) : replace lat_pat = lat_pat[1] (12 real changes made) . bysort hosp_ch (lat_pat) : replace lng_pat = lng_pat[1] (12 real changes made) . l+------------------------------------------------------------------------+| patien~d hosp_ch lat_pat lng_pat lat_hosp lng_hosp_fillin ||------------------------------------------------------------------------|1. | 2 a 1 3 56 0 |2. | 1 a 1 3 56 0 |3. | 9 a 1 3 56 0 |4. | 12 a 1 3 56 1 |5. | 8 a 1 3 56 1 ||------------------------------------------------------------------------|6. | 12 b 8 6 89 0 |7. | 2 b 8 6 89 1 |8. | 8 b 8 6 89 1 |9. | 1 b 8 6 89 1 |10. | 9 b 8 6 89 1 ||------------------------------------------------------------------------|11. | 8 c 3 5 1012 0 |12. | 12 c 3 5 1012 1 |13. | 2 c 3 5 1012 1 |14. | 1 c 3 5 1012 1 |15. | 9 c 3 5 1012 1 |+------------------------------------------------------------------------+Now comparisons between patient and possible hospital are allbetween two variables.Nick n.j.cox@durham.ac.uk -----Original Message-----From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Vitorino, Maria AnaSent: 21 October 2011 11:16 To: <statalist@hsphsun2.harvard.edu>Subject: Re: st: calculating many distances and storing them in manynew variablesDear Yuval,Thanks. But the goal is different. I know to which hospital eachpatient went. What I'm trying to do is to calculate the distancebetween each patient and each hospital alternative (ie all thehospitals that the patient could have decided to go to).I'm guessing this can be done with some sort of loop... AnaOn 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 withpatients, thehospital 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 toeverypossible hospital in the data. So, I would like 3 new columnswhich willcontain 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 anddist_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 wayaspossible. Any help is appreciated.* * 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/

**References**:**st: calculating many distances and storing them in many new variables***From:*Maria Ana Vitorino <vitorino@wharton.upenn.edu>

**Re: st: calculating many distances and storing them in many new variables***From:*Yuval Arbel <yuval.arbel@gmail.com>

**Re: st: calculating many distances and storing them in many new variables***From:*"Vitorino, Maria Ana" <vitorino@wharton.upenn.edu>

**RE: st: calculating many distances and storing them in many new variables***From:*Nick Cox <n.j.cox@durham.ac.uk>

- Prev by Date:
**Re: st: how to align the "baseline" (0) of 2 y axes?** - Next by Date:
**st: variable not found?** - Previous by thread:
**RE: st: calculating many distances and storing them in many new variables** - Next by thread:
**st: question about use of restricted cubic splines in regression** - Index(es):