DATA Step, Macro, Functions and more

Is there a way to get County using latitude and longitude?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Is there a way to get County using latitude and longitude?

I have files which contain data for addresses that have been geocoded.  They contain latitude and longitude.  Is there a way (a function or procedure) within SAS that will return the U.S. county from these coordinates?


Accepted Solutions
Solution
‎01-17-2014 03:10 PM
Super User
Posts: 17,828

Re: Is there a way to get County using latitude and longitude?

There's a proc for that!

SAS/GRAPH(R) 9.2: Reference, Second Edition

Ballardw suggest is probably the easiest and the example in the documentation is for example that problem.

If the projections don't match you can also find the proper version for  US county data online in many places and bring that into SAS.

View solution in original post


All Replies
Super Contributor
Posts: 307

Re: Is there a way to get County using latitude and longitude?

Have you taken a look at the SASHELP.ZIPCODE dataset?

Super Contributor
Posts: 307

Re: Is there a way to get County using latitude and longitude?

Ballard's suggestion may be preferable, but to elaborate further on my comment . . . .one could use GEODIST function to determine the closest location in the ZIPCODE dataset. This would be somewhat hit-and-miss, since the closest location may, in fact, be in a different county. Ideally, Ballard's approach makes better sense since your lat and long can only fall into unique counties (unless it happens to be exactly on a county boundary).

Super User
Posts: 10,500

Re: Is there a way to get County using latitude and longitude?

If you can get the lat and long projected (Proc Gproject) to match the USCounty map dataset provided by SAS then Proc Ginside should work.

Solution
‎01-17-2014 03:10 PM
Super User
Posts: 17,828

Re: Is there a way to get County using latitude and longitude?

There's a proc for that!

SAS/GRAPH(R) 9.2: Reference, Second Edition

Ballardw suggest is probably the easiest and the example in the documentation is for example that problem.

If the projections don't match you can also find the proper version for  US county data online in many places and bring that into SAS.

Occasional Contributor
Posts: 11

Re: Is there a way to get County using latitude and longitude?

Thanks for the link and suggestions. I haven't worked with geographic data before, so it is very helpful to be pointed in the right direction.

This questions was actually something I was asked by another programmer, so I'll forward the comments to her so she can try this out.

PROC Star
Posts: 7,363

Re: Is there a way to get County using latitude and longitude?

Just FWIW, if you enter a coordinate pair as a search string at www.mapquest.com, part of the output will contain the zipcode, city and state and, given that info, you can get the county name from sashelp.zipcode.

The following paper provides example code for automating such a lookup process:

http://support.sas.com/resources/papers/proceedings12/091-2012.pdf

To use it, you would have to get an API key from Mapquest, but that is free and easily done from their web site.

Trusted Advisor
Posts: 2,113

Re: Is there a way to get County using latitude and longitude?

One caution with Arthur's approach.  At least in NC, ZIP codes and City boundaries cross county lines.

Reeza's approach will have more precision.

Doc Muhlbaier

Duke

PROC Star
Posts: 7,363

Re: Is there a way to get County using latitude and longitude?

Doc: Before mentioned it, I wasn't even aware of proc ginside and it is definitely the easiest to implement. However, since both methods start with coordinates thus should be attempting to match with polygons, I wouldn't jump to the conclusion that one would necessarily be more accurate than the other.  Can you provide a couple of examples that we could compare?

Super User
Posts: 17,828

Re: Is there a way to get County using latitude and longitude?

Its not my suggestion, its Smiley Happy

But I will test it with Canadian data...out of curiousity. I have ArcGIS that can be the source of "truth"

Respected Advisor
Posts: 3,777

Re: Is there a way to get County using latitude and longitude?

You seem to forget the OP has long and lat for each address.  No need to involve ZIP code that I can see.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 2174 views
  • 3 likes
  • 7 in conversation