Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Graphics
- /
- Find a Central Zip for a Cluster of Zips

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-10-2015 10:59 AM
(1042 views)

Is there a way to find the central zip of a group of zips? For instance if I am given 30 different zips how can I find the central zip of that cluster?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Take the mean of the latitude and longitude and then find the zip that's the closest? You can find the closest by finding the distance between each zip and your centroid location and the one with the minimum distance would be the central.

GEODIST is a good function that would help with this.

Here's an example of how that might work, though it doesn't seem to pick the point I'd expect. Perhaps I did something wrong, but I would expect the methodology to work...

```
*Get random sample of zip codes;
data zips;
set sashelp.zipcode;
where statecode='NY';
if rand('bernoulli', 0.05)=1 then output;
keep zip x y;
run;
*Look at the spatial distribution;
proc sgplot data=zips;
scatter x=x y=y;
run;quit;
*Find centroid of all zips;
proc means data=zips mean noprint;;
var x y;
output out=zip_central mean(x)=mean_x mean(y)=mean_y;
run;
*Find distance from each zip to centroid;
data zips_all;
if _n_ =1 then set zip_central;
set zips;
distance=geodist(y, x, mean_y, mean_x);
distance2= ((x-mean_x)**2 + (y-mean_y)**2)**(0.5);
run;
*Sort so the closest zip to centroid is first;
proc sort data=zips_all;
by distance2;
run;
*Add in to main dataset;
data zip_central;
set zips /*original zip data*/
zip_central(rename=(mean_x=x mean_y=y)) /*center of zips*/
zips_all(obs=1 keep=zip x y) /*zip closest to centroid*/
indsname=source /*option to include datasource name*/;
dname=source;
run;
/*Plot to check answer*/
proc sgplot data=zip_central;
scatter x=x y=y/group=dname;
run;quit;
```

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Take the mean of the latitude and longitude and then find the zip that's the closest? You can find the closest by finding the distance between each zip and your centroid location and the one with the minimum distance would be the central.

GEODIST is a good function that would help with this.

Here's an example of how that might work, though it doesn't seem to pick the point I'd expect. Perhaps I did something wrong, but I would expect the methodology to work...

```
*Get random sample of zip codes;
data zips;
set sashelp.zipcode;
where statecode='NY';
if rand('bernoulli', 0.05)=1 then output;
keep zip x y;
run;
*Look at the spatial distribution;
proc sgplot data=zips;
scatter x=x y=y;
run;quit;
*Find centroid of all zips;
proc means data=zips mean noprint;;
var x y;
output out=zip_central mean(x)=mean_x mean(y)=mean_y;
run;
*Find distance from each zip to centroid;
data zips_all;
if _n_ =1 then set zip_central;
set zips;
distance=geodist(y, x, mean_y, mean_x);
distance2= ((x-mean_x)**2 + (y-mean_y)**2)**(0.5);
run;
*Sort so the closest zip to centroid is first;
proc sort data=zips_all;
by distance2;
run;
*Add in to main dataset;
data zip_central;
set zips /*original zip data*/
zip_central(rename=(mean_x=x mean_y=y)) /*center of zips*/
zips_all(obs=1 keep=zip x y) /*zip closest to centroid*/
indsname=source /*option to include datasource name*/;
dname=source;
run;
/*Plot to check answer*/
proc sgplot data=zip_central;
scatter x=x y=y/group=dname;
run;quit;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Ok great thank you. You got me on the right path.

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer.

** **

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.