Published on :
ETL EXTERNAL

OpenFiscalData API Extraction and Parsing

This code is also available in: Deutsch Español Français
Awaiting validation
The script initializes connection parameters (API key) and directory. A '%json' macro manages the extraction loop over a defined range of codes/dates. For each iteration, PROC HTTP retrieves JSON data. This data is parsed unconventionally using delimiters in a DATA STEP (reading as a flat file), followed by transposition to structure the data. Finally, a global step assembles the results and applies Korean column labels. The code contains hardcoded Windows paths and concatenation logic (L001_0-L001_200) which could fail if not all datasets in the range exist.
Data Analysis

Type : EXTERNAL


Data is extracted from the 'openapi.openfiscaldata.go.kr' API. The script attempts to write and reread temporary files in 'D:\OneDrive\...', which is incompatible with the current Linux SAS Viya environment without modifying the '&dir' path.

1 Code Block
MACRO
Explanation :
Initialization of environment variables, API key, and output library. The 'dir' path points to a Windows location.
Copied!
1%let dir=D:\OneDrive\Github\SAS-Projects\0002\;
2%let lib=json;
3%let String01=WBQMR1000052520180323030651FWHGU;/*apiKey*/
4LIBNAME &lib "&dir";
2 Code Block
PROC HTTP Data
Explanation :
Core processing: the macro iterates over requests, downloads JSON via PROC HTTP, and uses a manual parsing approach (input with JSON delimiters) to transform the hierarchical structure into a SAS table via PROC TRANSPOSE.
Copied!
1%macro json(data_final, String05, date_s, date_e);
2 %DO date_want=&date_s %to &date_e;
3 /* ... construction URL ... */
4 PROC HTTP out=out url="&url" method="post" ct="application/json";
5 RUN;
6 
7 DATA raw;
8 INFILE "&dir.SeriesDataOut.txt" dsd lrecl=999999999 dlm='{}[]:,';
9 INPUT raw : $2000. @code_sas_json/8_SAS_Intro_ReadFile_MultiCol_@@.json; /* Correction de l'artefact ' @code_sas_json...' */
10 IF _n_=5 THEN call symput('check', scan(raw,2));
11 RUN;
12 /* ... logique de transposition et merge ... */
13 %END;
14%mend;
15 
16%json(data_final=longdata_001, String05=http://openapi.openfiscaldata.go.kr/VWFOEM, date_s=161, date_e=163);
3 Code Block
DATA STEP Data
Explanation :
Final consolidation. Attempts to read a fixed range of tables (L001_0 to L001_200), applies Korean labels, and filters variables. Warning: this step will fail if the macro has not generated all tables from 0 to 200.
Copied!
1DATA &lib .longdata_001;
2 SET L001_0-L001_200;
3 label FSCL_YY= 회계년도;
4 label EXE_M =집행월;
5 /* ... autres labels ... */
6 keep &var_want;
7 IF FSCL_YY="" THEN delete;
8RUN;
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.