***************************************************************** * ksfe_2010_macro.SAS * * Geocoding with HTTP requests * * * * Bei Fragen und Anmerkungen melden Sie sich gerne * * * * Niko Sitaridis (nikolaos.sitaridis@uni-ulm.de) * * Gisela Büchele (gisela.buechele@uni-ulm.de) * * Jon Genuneit (jon.genuneit@uni-ulm.de) * *****************************************************************; libname input 'C:\KSFE\'; option mprint symbolgen; %macro geocode(data, breaklimit); option nonotes nomprint nosymbolgen; %local unit dsi n pos_url1 pos_url2 pos_id break record accuracy httpcode httpcode1 long lat elevation error1n error1c error2n error2c; %let unit=1; %let dsi=%sysfunc(open(&data, i)); %let n=%sysfunc(attrn(&dsi, nobs)); %let pos_url1=%sysfunc(varnum(&dsi, url1)); %let pos_url2=%sysfunc(varnum(&dsi, url2)); %let pos_id=%sysfunc(varnum(&dsi, id)); %do i=1 %to &n; %let break=0; %let record=; %let accuracy=.; %let httpcode=620; %let httpcode1=.; %let long=.; %let lat=.; %let elevation=.; %let error1n=.; %let error1c=; %let error2n=.; %let error2c=; %if %sysfunc(fetchobs(&dsi, &i))=0 %then %do; filename geocode1 url %sysfunc(compress(%sysfunc(getvarc(&dsi, &pos_url1)))); %if &sysfilrc=0 %then %do; %do %until(&httpcode ne 620 or &break gt &breaklimit); data _null_; infile geocode1; input; call symput('record', compress(_infile_)); stop; run; %let error1n=&syserr; %let error1c=&syserrortext; %let accuracy=%scan(%quote(&record), 2, %str(,)); %let httpcode=%scan(%quote(&record), 1, %str(,)); %if &httpcode=620 %then %do; %let break=%eval(&break + 5); %if &break le &breaklimit %then %syscall sleep(break, unit)); %end; %else %do; %if &httpcode=400 or &httpcode=602 %then %do; %let httpcode1=&httpcode; filename geocode2 url %sysfunc(compress(%sysfunc(getvarc(&dsi, &pos_url2)))); %if &sysfilrc=0 %then %do; %do %until(&httpcode1 ne 620 or &break gt &breaklimit); data _null_; infile geocode2; input; call symput('record',compress(_infile_)); stop; run; %let error1n=&syserr; %if &syserr ne 0 %then %let error1c=&syserrortext; %let accuracy=%scan(%quote(&record), 2, %str(,)); %let httpcode=%scan(%quote(&record), 1, %str(,)); %if &httpcode=620 %then %do; %let break=%eval(&break + 5); %if &break le &breaklimit %then %syscall sleep(break, unit)); %end; %end; filename geocode2 clear; %end; %end; %if &httpcode=200 %then %do; %let lat=%scan(%quote(&record), 3, %str(,)); %let long=%scan(%quote(&record), 4, %str(,)); filename geocode3 url "http://www.earthtools.org/height/&lat./&long"; %if &sysfilrc=0 %then %do; data _null_; infile geocode3 firstobs=8; input; call symput('elevation', scan(_infile_, 3, '<>')); stop; run; %let error2n=&syserr; %if &syserr ne 0 %then %let error2c=&syserrortext; filename geocode3 clear; %end; %else %put durchlauf (al): &i; %end; %end; %end; filename geocode1 clear; %end; %end; proc sql; %if &i=1 %then %do; create table geocodes (id num(4), error1n num(3), error1c char(255), error2n num(3), error2c char(255), break num(3), accuracy num(3), httpcode num(3), httpcode1 num(3), latitude num(6), longitude num(6), elevation num(3)); insert into geocodes values (%sysfunc(getvarn(&dsi, &pos_id)),&error1n, "&error1c", &error2n, "&error2c", &break, &accuracy, &httpcode, &httpcode1, &lat, &long, &elevation); %end; %else %do; insert into geocodes values (%sysfunc(getvarn(&dsi, &pos_id)),&error1n, "&error1c", &error2n, "&error2c", &break, &accuracy, &httpcode, &httpcode1, &lat, &long, &elevation); quit; %end; %end; %let dsi=%sysfunc(close(&dsi)); option notes; %mend geocode; option nomprint nosymbolgen; %geocode(input.arena, 15);