Hi all,

I use Stata 17 to create a spatial panel dataset of more than 3000 US counties and 5 time periods. So far following Stata 17 documentation, I have converted shapefile into .dta file. Please see the following code. I am unable to create a spatial weight matrix that is compatible with panel data.

Code:
*Spatial files
cd "J:\Data_Irrigation\Spatial"

*Creating Stata-format shapefile
*spshape2dta "J:\Data_Irrigation\Spatial\ContiguousUS",  saving(Counties) replace  //Don't use this if the files are created. Takes a lot of time to recreate.

*Using dta shapefiles
use "J:\Data_Irrigation\Spatial\Counties.dta", clear
spset
destring GEOID, gen(geoid)

drop fips
*Create a standard ID variable
gen long fips = real(STATEFP + COUNTYFP)

*Assert ID variable
bysort fips: assert _N==1
assert fips != .

*Setting a standard ID variable
spset fips, modify replace

*Set coodinate units
spset, modify coordsys(latlong, miles)

save, replace

*Preparing data for merging with Stata-format file
*Loading panel data
use "J:\Data_Irrigation\Irrigation_LL.dta", clear


*Creating a time variable
gen time = 1 if year==1995
replace time = 2 if year==2000
replace time = 3 if year==2005
replace time = 4 if year==2010
replace time = 5 if year==2015

xtset id time

*To make a balanced data
drop if state == "#N/A"
drop if lat =="#N/A"
drop if geoid >70000
drop if panelid==""

*filling missing  panel observations
tsset id time
tsfill, full

*Verfiying that id and time are jointly an ID variables
assert id!=.
assert time!=.
bysort id time: assert _N==1

*Balancing and spset the data
spbalance
spset id

save  , replace

spmatrix create contiguity M, norm(row)
I get an error message

Sp data not linked to shapefile
Type spset, modify filename(<filename>_shp.dta) to link a _shp.dta file to the dataset in memory.
r(601);

end of do-file
r(601);
Code:
. spset, modify shpfile(Counties_shp.dta)
variable _ID contains invalid values
_ID contains 3074 values that do not appear in Counties_shp.dta. If you want to drop not-on-the-map observations, use spset with option drop.

I cannot drop counties from Counties_shp.dta because then only a few counties will be left.

I am doing something wrong related to choosing the right .dta file generated using -spshape2dta- .

Can somone please help correct this?


Thanks,

Ritika