BookmarkSubscribeRSS Feed
dwsmith
Obsidian | Level 7

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;
    datalines;
    1 57 -112
    2 30.2 -97.7
    ;
run;

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

1 REPLY 1
jklaverstijn
Rhodochrosite | Level 12

Hi,

 

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=https://maps.googleapis.com/maps/api/geocode/json?latlng=&lat.,&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.

 

Regards,

- Jan.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1097 views
  • 3 likes
  • 2 in conversation