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.