Published on :
ETL EXTERNE

Korean Fiscal Data Extraction and Parsing via API

This code is also available in: Deutsch Español Français
Awaiting validation
This script configures a local library and defines a '%json' macro to extract data. It iterates over a range of years and pages, performs POST requests via PROC HTTP to 'openapi.openfiscaldata.go.kr', and saves the raw response. JSON parsing is performed in a non-standard way via a DATA step using specific delimiters and transposition. Finally, a post-processing step applies Korean labels and filters variables.
Data Analysis

Type : EXTERNE


Data dynamically retrieved via the 'openapi.openfiscaldata.go.kr' (Open Fiscal Data) API in JSON format.

1 Code Block
DATA STEP
Explanation :
Initialization of global variables (path, API key) and definition of the output library.
Copied!
1%let dir=C:\json\;
2%let lib=json;
3%let String01=WBQMR1000052520180323030651FWHGU;/*apiKey*/
4 
5LIBNAME &lib "&dir";
2 Code Block
PROC HTTP Data
Explanation :
Dynamic URL construction with year and pagination parameters, then execution of a POST request to download raw data to a text file.
Copied!
1%macro json(data_final, String05, date_s, date_e);
2 /* ... boucle date ... */
3 %let url=&string05?FSCL_YY=&date_want&key=&string01&type=json&pindex=&string02&psize=1000;
4 
5 filename out "&dir.SeriesDataOut.txt" recfm=v lrecl=999999999;
6 PROC HTTP out=out url="&url" method="post" ct="application/json";
7 RUN;
3 Code Block
DATA STEP Data
Explanation :
Manual import and parsing of the JSON file (treated as delimited text). The 'merge' logic with offset (firstobs=2) likely serves to associate JSON keys and values.
Copied!
1 DATA raw;
2 INFILE "&dir.SeriesDataOut.txt" dsd lrecl=999999999 dlm='{}[]:,';
3 INPUT raw : $2000. /* ... */;
4 RUN;
5 
6 DATA temp;
7 MERGE raw raw(firstobs=2 rename=(raw=_raw));
8 IF mod(_n_,2) eq 0;
9 RUN;
10 /* ... logique de groupe ... */
4 Code Block
PROC TRANSPOSE Data
Explanation :
Pivots the parsed data to transform key-value pairs (rows) into structured columns, then appends this data to the cumulative final table.
Copied!
1 PROC TRANSPOSE DATA=temp out=data_one(drop=_:);
2 BY group;
3 id raw;
4 var _raw;
5 RUN;
6 
7 DATA &lib..&data_final;
8 SET &lib..&data_final data_one;
9 RUN;
5 Code Block
DATA STEP Data
Explanation :
Final table cleaning: deletion of empty rows, application of column labels (in Korean) for business documentation, and retention of only the variables of interest defined in &var_want.
Copied!
1/*변수명, 변수 형태 변경_start*/
2DATA &lib .longdata_002;
3SET &lib .longdata_002;
4IF FSCL_YY="" THEN delete;
5label FSCL_YY=회계연도;
6/* ... autres labels ... */
7label NRC_AMT =미수납액(원);
8keep &var_want;
9RUN;
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.