I have a dataset with individuals with their geocoordinates and supermarket branches with geocoordinates. I need to calculate distance between the house of each individual to all the supermarket branches operating on the year of birth of that individual.
I know the format of geodist and geonear but I don't know how to loop it for each individuals year of birth to each supermarket's branch year.
For example:
clear
set obs 10
gen lat1 = 37 + (41 - 37) * uniform()
gen lon1 = -109 + (109 - 102) * uniform()
gen yrbirth= round(1950 + (12-5) * uniform())
save "individual.dta", replace
clear
set obs 15
gen lat2 = 37 + (41 - 37) * uniform()
gen lon2 = -109 + (109 - 102) * uniform()
gen stryr= round(1950 + (12-5) * uniform())
save "store.dta", replace
merge 1:1 _n using "individual.dta", nogen
Now, in this example, individual 1 with year of birth 1952 does not have any supermarket branch operating in that year so I want the distance to be (.). For the individual with year of birth 1955, I want to calculate the distance to the 7 supermarket branches operating in 1955. I want to do it for all individuals and then keep for each individual the nearest two supermarket branches.
I would appreciate if someone can help me.
Related Posts with Looping with Geodist
Student and Subject Fixed effectsHi, I am running a panel data with 3000 observations and each student has two subjects recorded. I…
Remove accents on a string variable in stataDear everyone, I would like to know if someone knows a STATA code that I can use to remove accents …
Why Stata doesn't recognize empty valuesHi, I have recently created a new variable mothergrad1 and there are empty values when I used the f…
Sum Variable in columsHello i have id Year temp "seq" 1 2001 . 0 1 2002 1 1 1 2003 1 2 1 2004 . 0 2 2001…
Why the local value in a loop changes after use another data-set ?I have two datasets. The first one contains the following data. The data name is Forloop.dta. Code:…
Subscribe to:
Post Comments (Atom)
0 Response to Looping with Geodist
Post a Comment