0
votes

Is it possible to run the command georoute for multiple values at the same time? I'm running the following line:

 georoutei, hereid(BfSfwSlKMCPHj5WbVJ1g) herecode(bFw1UDZM3Zgc4QM8lyknVg) startxy(46.1761413,6.1393099) endxy(46.99382,6.94049) km

However, I have more than 1000 values (coordinates) as a start and end point. I'd like to tell Stata something like replace startxy and endxy for each value of the variables. So, I'm trying to do this across a loop but it doesn't work. Can someone help me?

I'm trying to do this:

clear all

set obs 1000
set seed 1254
ssc install georoutei

gen x1 = 45 + int(40+1)*uniform() 
gen xf = 46+ int(46+1)*runiform()
gen y1 = 6 + int(6+1)*runiform()
gen yf = 7 + int(7+1)*runiform()

georoutei, hereid(tKNXdBlaC1UoKDWAiJ4i) herecode(4SQsFZphiBfXL4xWa7SPgg) 
startxy(3.4372222,-76.33376) endxy(3.7231,-76.525902) km

forvalue i=1(1)1000{
levelsof x1, local(startx)
levelsof xf, local(endf) 
levelsof y1, local(starty)
levelsof yf, local(endy)
    georoutei, hereid(tKNXdBlaC1UoKDWAiJ4i) herecode(4SQsFZphiBfXL4xWa7SPgg) 
    startxy(`startx',`starty') endxy(`endx',`endy')km
}

Then I need to record the travel distance in a matrix or something like this to have a new variable with this information. I'm looking to save a little time instead of having to run a bunch of lines for each pair of coordinates.

Thank you!

1
Can you post what you tried for the loop?Matthew White
Hello Matthew, I've edited the post, maybe the question can be easier to understand.Carolina Bermudez

1 Answers

1
votes

The issue is that each iteration of the loop is running the same levelsof command: there is no if or in qualifier specified to levelsof, so for each iteration of the loop, levelsof is running for all observations.

Instead of levelsof, I recommend using explicit subscripting. You can read more about it here and here. For example:

generate distance = .
forvalues i = 1/100 {
    local startx = x1[`i']
    local endf = xf[`i']
    local starty = y1[`i']
    local endy = yf[`i']
    georoutei, hereid(tKNXdBlaC1UoKDWAiJ4i) herecode(4SQsFZphiBfXL4xWa7SPgg) startxy(`starty',`startx') endxy(`endy',`endx') km
    replace distance = r(dist) in `i'
}

Definitely a good idea to turn this into a loop!