/******************* URBAN INSTITUTE MACRO LIBRARY *********************
: Get_census_api
Macro: Read Census data through a JSON API service into a
Description9.4M4 or later.
SAS data set. Requires SAS
: Open code
Use
: Peter Tatian
Author
***********************************************************************/
Get_census_api(
%macro api=, /** API path (quoted character string) **/
out= /** Output data set (optional) **/
);
/*************************** USAGE NOTES *****************************
:
SAMPLE CALLGet_census_api(
%api='https://api.census.gov/data/2017/acs/acs5?get=B01001_001E,NAME&for=tract:*&in=state:01&in=county:*',
out=alabama_tracts
)for ACS 5-year 2017 tract population for
Reads Census API
Alabama and outputs to data set work.alabama_tracts.*********************************************************************/
/*************************** UPDATE NOTES ****************************
5/4/2019 Code adapted from https://acsdatacommunity.prb.org/acs-data-products--resources/api/f/15/t/388
*********************************************************************/
***** ***** ***** MACRO SET UP ***** ***** *****;
%
note_mput( macro=Get_census_api, msg=Macro starting. )
%
***** ***** ***** ERROR CHECKS ***** ***** *****;
%
%if %length( &api ) = 0 %then %do;
err_mput( macro=Get_census_api, msg=Must provide an API= value. )
%
%goto exit;
%end;
***** ***** ***** MACRO BODY ***** ***** *****;
%
filename _rapirsp temp;
proc http=&api
url="GET"
method=_rapirsp;
out
run;
/* puts the results from a responsefile into a table and
*/
uses the first row from the response to create variable names
=_rapirsp;
libname _rapitmp JSON fileref
data _null_;_rapitmp.root (obs=1);
set *} element:;
array elemCols{$4000;
length rename =1 to dim(elemCols);
do i=catx(' ',rename, catx('=','element'||compress(put(i,3.)),elemCols{i}));
rename
end;symputx('rename',rename);
call
run;
&out;
data _rapitmp.root (firstobs=2);
set &rename;
rename
run;
***** ***** ***** CLEAN UP ***** ***** *****;
%
libname _rapitmp clear;
filename _rapirsp clear;
:
%exit
note_mput( macro=Get_census_api, msg=Macro exiting. )
%
%mend Get_census_api;
/************************ UNCOMMENT TO TEST ***************************
** Locations of SAS autocall macro libraries **;
"K:\Metro\PTatian\UISUG\Uiautos";
filename uiautos =(uiautos sasautos);
options sasautos
options mprint nosymbolgen nomlogic;** Check error handling **;
Get_census_api( )
%** Check reading API: all 2017 5-year Alabama tract populations **;
Get_census_api(
%api='https://api.census.gov/data/2017/acs/acs5?get=B01001_001E,NAME&for=tract:*&in=state:01&in=county:*',
out=alabama_tracts
)
proc contents;
proc print;
run;
/**********************************************************************/
Census API
Purpose: Read Census data through a JSON API service into a SAS data set. Requires SAS 9.4M4 or later.