Created
August 27, 2014 06:13
-
-
Save FriedEgg/e82bde6e96dba8244b5e to your computer and use it in GitHub Desktop.
Another implementation of using Google geocoding API in SAS, this time using PROC FCMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc fcmp outlib=work.func.api; | |
subroutine geocode(address $, adr $, lat, lng); | |
outargs adr, lat, lng; | |
api_key="<<YOUR API KEY>>"; | |
length url c adr $ 32767 prxadr prxlat prxlng lat lng 8 fileref $ 8; | |
prxadr=prxparse('/\"formatted_address\"\ :\ \"([^\"]*)\"/'); | |
prxlat=prxparse('/\"lat\"\ \:\ ([-\d\.]*)/'); | |
prxlng=prxparse('/\"lng\"\ \:\ ([-\d\.]*)/'); | |
url='https://maps.googleapis.com/maps/api/geocode/json?address=' || urlencode(strip(address)) || '&key=' || strip(api_key); | |
fileref=''; | |
rc=filename(fileref, strip(url), 'url'); | |
put fileref; | |
if rc then do; | |
msg=sysmsg(); | |
put msg; | |
end; | |
else do; | |
fid=fopen(fileref, 's'); | |
if not fid then do; | |
msg=sysmsg(); | |
put msg; | |
end; | |
else do; | |
do while (fread(fid)=0); | |
rc=fget(fid,c,32767); | |
put c; | |
if prxmatch(prxadr, c) then adr=prxposn(prxadr, 1, c); | |
else if prxmatch(prxlat, c) then lat=input(prxposn(prxlat, 1, c), best.); | |
else if prxmatch(prxlng, c) then lng=input(prxposn(prxlng, 1, c), best.); | |
put adr lat lng; | |
if missing(adr)+missing(lat)+missing(lng)=0 then return; | |
end; | |
end; | |
rc=fclose(fid); | |
end; | |
endsub; | |
run; | |
options cmplib=work.func; | |
data foo; | |
input address $60.; | |
length _address $60. lat lng 8; | |
call geocode(address, _address, lat, lng); | |
*a list of SAS offices in California for example data; | |
cards; | |
5 Park Plaza, Suite 900 Irvine, CA 92614 | |
6700 Koll Center Pkwy Ste 120 Pleasanton, CA 94566-7032 | |
1215 K St Ste 1100 Sacramento CA 95814-3945 | |
10188 Telesis Court, Suite 200 San Diego, CA 92121 | |
One Montgomery Street, 34th Floor San Francisco, CA 94104 | |
15300 Ventura Blvd Ste 523 Sherman Oaks CA 91403-5845 | |
; | |
run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is important to note that this example does not fully comply with the terms of use as it does not include code to display the information on a google map. This gist specifically only details the interaction with the geocoding web service.
https://developers.google.com/maps/terms#section_10_12