SASでJSONデータを取り込む

今回はSASJSONデータを取り込む例を紹介します。

 

JSON形式のデータはPROC HTTPとJSON形式のライブラリ参照で取得することができます。

例:

------------------------------------------------------------------------------------------

filename resp 〈ファイル参照名〉;

 

proc http url='<JSON-URL>' method="GET" out=resp;

run;

 

libname space JSON fileref=resp;

------------------------------------------------------------------------------------------

 

上の例ではSPACEという名前のライブラリ名でJSON形式のライブラリに関連するデータセットが作成されます。今回は試しにWHOのサイトからCOVID19データを JSON形式でダウンロードして月別の感染者の最大増加を表すデータセットを作成してみます。

COVID19のJSON形式のファイルはourworldindata.orgからダウンロード可能です。

 

ourworldindata.org

 

上のURLのJSONリンクをクリックするとURLが表示されますので先ほどのコードに張り付けて実行することでSPACEという名前のライブラリにCOVID関連のデータセットが表示されます。

 

------------------------------------------------------------------------------------------

filename resp 〈ファイル参照名〉;

 

proc http url='https://covid.ourworldindata.org/data/owid-covid-data.json' method="GET" out=resp;

run;

 

libname space JSON fileref=resp;

------------------------------------------------------------------------------------------

 

f:id:essbase:20201112124250p:plain

 

 

 

filename resp temp;

 

proc http url='https://covid.ourworldindata.org/data/owid-covid-data.json'

method="GET" out=resp;

run;

 

/* Assign a JSON library to the HTTP response */

libname space JSON fileref=resp;

*assign country;

%let cntry=JPN;

 

data have;

drop ordinal:;

retain year month day _date dif_new_cases;

set space.&cntry._data;

by date;

format _date yymmdds10.;

dif_new_cases=dif(new_cases);

_date=input(date, yymmdd10.);

year=year(_date);

month=month(_date);

day=day(_date);

output;

drop date;

rename _date=date;

run;

 

data out;

length year month day max_dif_new_cases max_dif_new_cases_date max_new_cases

max_new_cases_date max_new_deaths max_new_deaths_date max_stringency_index

max_stringency_index_date 8;

format date max_dif_new_cases_date max_new_cases_date max_new_deaths_date

max_stringency_index_date yymmdds10.;

 

do until(last.month);

set have end=lr curobs=_r;

by month notsorted;

 

if max_dif_new_cases < dif_new_cases then

do;

max_dif_new_cases=dif_new_cases;

max_dif_new_cases_date=date;

end;

 

if max_new_cases < new_cases then

do;

max_new_cases=new_cases;

max_new_cases_date=date;

end;

 

if max_new_deaths < new_deaths then

do;

max_new_deaths=new_deaths;

max_new_deaths_date=date;

end;

 

if max_stringency_index < stringency_index then

do;

max_stringency_index=stringency_index;

max_stringency_index_date=date;

end;

end;

drop new_cases-numeric-new_tests_per_thousand tests_units;

run;