DATA Step, Macro, Functions and more

determine country name from GPS

Posts: 40

determine country name from GPS

If I have a dataset with GPS locations, can I determine the country these coordinates are in?


For example, consider the following dataset:

data test;
    input sn lat long;
    1 57 -112
    2 30.2 -97.7

How can I add a new column extracting the country name from gps if possible?

Valued Guide
Posts: 533

Re: determine country name from GPS



Google offers a web service for their maps API to do just that.  For low volume queries they do not charge you anything but you will have to register and obtain a key. Yoou can use proc http to send the request and parse out the json returned result in a datastep.


This is the code I use. Use macro processing to cycle through a list of coordinates. Perhaps it's not the quickest way and can probably be improved (hope someone will point out how) but it was surely fun to see it work. For my purpose, finding start and end points of my tracked car trips, it is more than sufficient.


%let lat=35.828713;
%let lng=-78.767082;
%let url=,&lng.%str(&)location_type=ROOFTOP%str(&)result_type=street_address%str(&)key=wouldntyouliketoknow; proc http out=location method='get' url="&url" headerout=hdrs; run; data address; infile location; length rec property formatted_address $512; start= 1; keep latitude longitude formatted_address; retain re formatted_address; if _n_=1 then do; re=prxparse('/"[^"]*"/'); end; input; rec = _infile_; call prxnext(re, start, -1, rec, position, l); if position > 0 then do; property = substr(rec, position+1, l-2); if strip(property)="formatted_address" then do; call prxnext(re, start, -1, rec, position, l); formatted_address = substr(rec, position+1, l-2); latitude = ⪫ longitude = &lng; put latitude= longitude= formatted_address=; output; stop; end; end; run;

This prints out

latitude=35.828713 longitude=-78.767082 formatted_address=100 SAS Campus Dr, Cary, NC 27513, USA

As I said perhaps not the quickest way but it was surely fun to see it work. And the Google API is very verstile.



- Jan.

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation