Sample 22872: Determine which counties border a specific county
There is not a function in SAS to list neighboring counties for a specific county. However, if you assume that each county neighbor will have at least one coordinate in common with the specified county, you can use the code on the Full Code tab to determine the neighboring counties.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample will determine the neighboring counties when the bordering counties share at least one point in common.
Note that if a body of water separates the polygons, a common point will not be found.
/* Assuming that each bordering polygon shares at least one
common point, determine the neighboring counties for a
given county */
/* Define the state and county FIPS codes of interest */
%let st=37;
%let cnty=185;
/* Determine state and county name */
data _null_;
set maps.cntyname;
where state=&st and county=&cnty;
call symputx('stname',fipnamel(&st));
call symputx('cntyname',propcase(countynm));
run;
/* Create a data set for the desired state and county */
data state;
set maps.uscounty;
where state=&st and county=&cnty;
run;
/* Create a data set of all the other counties */
data others;
set maps.uscounty;
/* if you only want counties within the selected state */
/* add state=&st to the WHERE clause */
where county ^= &cnty;
run;
/* Use PROC SQL to do an inner join */
/* where the X and Y variables from the state data set */
/* match the X and Y variables for the other states */
/* save the matches in the BORDER data set */
proc sql;
create table border as
select others.state, others.county
from state, others
where state.x = others.x and state.y = others.y;
quit;
/* Remove any duplicate county observations */
proc sort data=border nodupkey;
by state county;
run;
quit;
/* Add state and county names to the data set */
data border;
merge border(in=a) maps.cntyname(keep=state county countynm);
by state county;
if a;
stname=fipnamel(state);
countynm=propcase(countynm);
label stname='State Name' countynm='County Name';
run;
/* View the results */
title "Neighboring counties for &cntyname county &stname ";
proc print data=border noobs label;
var stname countynm;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Neighboring counties for Warren county North Carolina
State Name County Name
North Carolina Franklin
North Carolina Halifax
North Carolina Nash
North Carolina Northampton
North Carolina Vance
Virginia Brunswick
Virginia Mecklenburg
This sample will determine the neighboring counties when the bordering counties share at least one point in common.
Note that if a body of water separates the polygons, a common point will not be found.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> GMAP
|
Date Modified: | 2013-02-07 14:31:28 |
Date Created: | 2002-12-16 10:56:43 |
Operating System and Release Information
SAS System | SAS/GRAPH | All | n/a | |