/*** TIP00395 ***/
*** Determine what States are adjacent to each other;

Solution by 
Mike Zdeb
U@Albany School of Public Health
1 University Drive
Rensselaer, NY   12144-3456
(P)518-402-6479

*** merge a SAS/GRAPH map data set with itself;
*** find common vertices (lat/long locations with same values);

proc sql;
create table borders as
select distinct d0.state as state0,
                d1.state as state1
from maps.states d0, maps.states d1
where (d0.x eq d1.x and d0.y eq d1.y);
quit;

*** extra data step to eliminate state bordering itself;
*** also adds state names to the data set;

data borders;
set borders;
by state0;
if state0 eq state1 then state1 = .;
if not (first.state0 and last.state0) and state1 eq . then delete;
stname0 = fipname(state0);
stname1 = fipname(state1);
run;

proc print data=borders;
var state1 stname1;
by state0 stname0;
run;

*** rearrange data to create a matrix of bordering states;

proc transpose data=borders out=new (drop=_:) prefix=state;
by state0;
run;

proc print data=new;
run;


*** alternative --- eliminates states bordering itself, but also
*** eliminates states that border no other states (ALASKA, HAWAII, PUERTO RICO);
*** merge a SAS/GRAPH map data set with itself;
*** find common vertices (lat/long locations with same values);

proc sql;
create table borders as
select distinct d0.state as state0,
                d1.state as state1
from maps.states d0, maps.states d1
where (d0.x eq d1.x and d0.y eq d1.y) and state0 ne state1;
quit;

/*** end of tip 00395 ***/