I have a dataset like this,
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str1 district str9 dates float(district_lat district_lon) byte population "a" "2021/10/1" 22.248306 114.15244 10 "a" "2021/10/2" 22.248306 114.15244 20 "a" "2021/10/3" 22.248306 114.15244 30 "a" "2021/10/4" 22.248306 114.15244 40 "b" "2021/10/1" 22.279636 114.1655 10 "b" "2021/10/2" 22.279636 114.1655 20 "b" "2021/10/3" 22.279636 114.1655 30 "b" "2021/10/4" 22.279636 114.1655 40 "b" "2021/10/5" 22.279636 114.1655 50 "c" "2021/10/1" 22.24186 114.15285 10 "c" "2021/10/2" 22.24186 114.15285 20 "d" "2021/10/1" 22.28582 114.1998 30 "e" "2021/10/1" 22.28598 114.1915 20 "e" "2021/10/2" 22.28598 114.1915 30 "f" "2021/10/1" 22.27999 114.1588 60 "g" "2021/10/2" 22.26781 114.23608 50 "h" "2021/10/2" 22.335806 114.1495 40 "i" "2021/10/4" 22.33608 114.20561 30 end
First, I calculate the distance between i and j and generate d_ij
Second, I check if the distance between i and j (d_ij) is smaller than 1km
Third, if the distance between i and j (d_ij) is smaller than 1km, I calculate the inverse of the distance times the population density in location j, generate density=(1/d_ij)*populationj
Fourth, for location i, I sum up all the densities generated.
Fifth, the population of a location is changing each day and I need to do the calculation for location i in each day.
I have a code that looks like this
forval i = 1/`=_N' {
local olat = latitude[`i']
local olong = longitude[`i']
local pop=population[`i']
geodist latitude longitude `olat' `olong', gen(dist`i')
replace dist`i'=. if dist`i'>1
replace dist`i'=0.1 if dist`i'<0.1
replace dist`i'=1/dist`i'*`pop' if dist`i'<1
}
local olat = latitude[`i']
local olong = longitude[`i']
local pop=population[`i']
geodist latitude longitude `olat' `olong', gen(dist`i')
replace dist`i'=. if dist`i'>1
replace dist`i'=0.1 if dist`i'<0.1
replace dist`i'=1/dist`i'*`pop' if dist`i'<1
}
Thank you very much for your time!
0 Response to How to loop using date
Post a Comment