Skip to content

Instantly share code, notes, and snippets.

@FriedEgg
Created August 27, 2014 04:18
Show Gist options
  • Save FriedEgg/eed6c08344acc85ee5fc to your computer and use it in GitHub Desktop.
Save FriedEgg/eed6c08344acc85ee5fc to your computer and use it in GitHub Desktop.
An interface for using Google Geocode API with PROC GROOVY in SAS
%let api_key=<<YOUR GOOGLE API_KEY>>;
filename cp temp;
filename ivy "%sysfunc(pathname(work,l))/ivy.jar";
proc http
method = 'get'
url = 'http://central.maven.org/maven2/org/apache/ivy/ivy/2.3.0-rc1/ivy-2.3.0-rc1.jar'
out = ivy
;
run;
proc groovy classpath=cp;
add classpath=ivy;
add sasjar="groovy_2.1.3" version="2.1.3.0_SAS_20130517000930";
submit parseonly;
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.GET
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
class GoogleGeocodeApi {
static def geocode(String api_key, String address) {
LinkedHashMap geometry
def address_url = URLEncoder.encode(address)
def url = "https://maps.googleapis.com/maps/api/geocode/json?address=${address_url}&key=${api_key}"
def api = new HTTPBuilder(url)
api.request(GET, JSON) { req ->
response.success = { resp, json ->
json.results.each {
geometry = [
formatted_address: it.formatted_address,
latitude: it.geometry.location.lat,
longitude: it.geometry.location.lng
]
}
}
response.failure = { resp, json ->
println "Unexpected error: ${resp.status} : ${resp.error_message}"
resp.headers.each { println "${it.name} : ${it.value}" }
}
}
return geometry
}
}
endsubmit;
submit parseonly;
import java.util.LinkedHashMap;
import java.math.BigDecimal;
public class GoogleGeocode4SAS {
public String api_key = "";
public String address = "";
public void main() {
GoogleGeocodeApi api = new GoogleGeocodeApi();
geo = ((LinkedHashMap) GoogleGeocodeApi.geocode(api_key, address));
}
public String getFormattedAddress() {
return geo.get("formatted_address").toString();
}
public double getDouble(String key) {
BigDecimal bd = ((BigDecimal) geo.get(key));
return bd.doubleValue();
}
public double getLatitude() {
return getDouble("latitude");
}
public double getLongitude() {
return getDouble("longitude");
}
protected LinkedHashMap geo;
}
endsubmit;
quit;
data geocode;
if _n_=1 then do;
dcl javaobj geo("GoogleGeocode4SAS");
geo.setStringField("api_key", "&api_key.");
end;
input address $60.;
geo.setStringField("address", address);
geo.callVoidMethod("main");
length formatted_address $ 200 latitude longitude 8;
geo.callStringMethod("getFormattedAddress", formatted_address);
geo.callDoubleMethod("getLatitude", latitude);
geo.callDoubleMethod("getLongitude", longitude);
*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;
@rudvfaden
Copy link

Do you have an example where you sign ussing HMAC-SHA1 instead of using an api key?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment