Data is entirely retrieved from an external source via calls to the KOSIS (Korean Statistical Information Service) web API. The script does not depend on pre-existing data.
1 Code Block
%LET / LIBNAME
Explanation : Initialization of global macro variables. 'lib' defines the output path, 'String01' contains an API key, 'var_want' lists the columns to keep. A 'json' libname is assigned to the output path to save SAS tables.
Copied!
%let lib=C:\Json\;
%let String01=NjdhZTg3ZTM1OGEzZGMyOGIyZWE0ZmIxZTBiMDg0ZTg=;/*apiKey*/
%let var_want=PRD_DE/*연도*/ C1 C1_NM C3_NM/*연령*/ DT/*수*/;/*원하는 변수만 남기기, 전부 남기고 싶으면 공란*/
libname json "&lib";
%let var_want=PRD_DE/*연도*/ C1 C1_NM C3_NM/*연령*/ DT/*수*/;/*원하는 변수만 남기기, 전부 남기고 싶으면 공란*/
4
LIBNAME json "&lib";
2 Code Block
Macro Data
Explanation : Definition of the '%json' macro. This macro iterates over a date range, constructs a URL for the KOSIS API at each iteration, executes a PROC HTTP to retrieve JSON data, reads it with LIBNAME JSON, transforms it with PROC TRANSPOSE to pivot it, and accumulates it into a final SAS table.
Copied!
%macro json(data_final, String02, String03, String04, String05, String06, String07, String08, String09, String10, String11, String12, String13, String14, date_s, date_e);
data "&lib.&data_final";run;
%do date_want=&date_s %to &date_e;
%let url=http://kosis.kr/openapi/Param/statisticsParameterData.do?method=getList&apiKey=&String01&itmId=&String12&objL1=&String04&objL2=&String05&objL3=&String06&objL4=&String07&objL5=&String08&objL6=&String09&objL7=&String10&objL8=&String11&format=json&jsonVD=Y&prdSe=&String14&startPrdDe=&date_want&endPrdDe=&date_want&loadGubun=&String13&orgId=&String02&tblId=&String03;
filename out temp;proc http url="&url" method="get" out=out;run;
libname raw json fileref=out;
data "&lib.temp";set raw.alldata;if p1='TBL_NM' then group+1;/*자동으로 p1의 첫번째 값을 'TBL_NM'대신 들어가도록 하는 방법을 못 찾겠습니다.*/run;
proc transpose data="&lib.temp" out="&lib.data_one"(drop=_:);by group;id P1;var Value;run;
data "&lib.data_one";set "&lib.data_one"(keep=&var_want);run;
data "&lib.&data_final";set "&lib.&data_final" "&lib.data_one";run;
%end;
data "&lib.&data_final";set "&lib.&data_final";if _n_=1 then delete;run;
%mend;
Explanation : Call to the '%json' macro twice. The first call downloads statistical data at the state level ('data_state'). The second call downloads data at the city level ('data_city'). Both extractions cover the period from 1993 to 2017.
Explanation : This final DATA STEP modifies the 'json.data_city' table. It renames columns with more explicit names (Year, Div, Old, Num), converts numeric variables stored as text to a numeric format, and selects the final columns for analysis.
Copied!
data json.data_city;
set json.data_city;
Year=PRD_DE+0;/*연도*/
Div_num=C1;
Div=C1_NM;
Old=C3_NM;
Num=DT+0;/*수*/
keep Year Div_num Div Old Num;
run;
1
DATA json.data_city;
2
SET json.data_city;
3
Year=PRD_DE+0;/*연도*/
4
Div_num=C1;
5
Div=C1_NM;
6
Old=C3_NM;
7
Num=DT+0;/*수*/
8
keep Year Div_num Div Old Num;
9
RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.