Published on :
Test / Macro CREATION_INTERNE

Test of the mp_jsonout macro with special missing values

This code is also available in: Deutsch Español Français
Awaiting validation
The script creates a dataset containing a series of numerical values, including special missing values (._, .a, .b, etc.). It uses the mp_jsonout macro to export this data to JSON. The resulting file is then re-read and converted (special values being treated as text in the JSON) to verify that the reconstructed data exactly matches the original data via PROC COMPARE.
Data Analysis

Type : CREATION_INTERNE


The 'demo' table is generated locally via an iterative Data Step.

1 Code Block
DATA STEP Data
Explanation :
Definition of a temporary file and creation of the 'demo' data table containing various numerical values and special missing values for testing.
Copied!
1filename webref temp;
2 
3DATA demo;
4 DO x=._,.,.a,.b,.c,.d,.e,-99, 0, 1,2, 3.333333;
5 OUTPUT;
6 END;
7RUN;
2 Code Block
MACRO CALL
Explanation :
Call to the mp_jsonout macro to export the 'demo' table to the temporary file 'webref', with the missing=STRING option to handle specific formats.
Copied!
1%mp_jsonout(OPEN,jref=webref)
2%mp_jsonout(OBJ,demo,jref=webref,fmt=N,missing=STRING)
3%mp_jsonout(CLOSE,jref=webref)
3 Code Block
DATA STEP
Explanation :
Debugging step displaying the raw content of the generated JSON file in the SAS log.
Copied!
1DATA _null_;
2 INFILE webref;
3 INPUT;
4 putlog _infile_;
5RUN;
4 Code Block
DATA STEP Data
Explanation :
Reading the JSON file via the LIBNAME JSON engine and reconstructing the 'test' table. Includes logic to convert character strings (representing special missing values) back into SAS numerical format.
Copied!
1LIBNAME web JSON fileref=webref;
2 
3DATA work.test(keep=x);
4 SET web.demo(rename=(x=y));
5 IF y ='_' THEN x=._;
6 ELSE IF anyalpha(y) THEN x=INPUT(cats(".",y),best.);
7 ELSE x=INPUT(y,best.);
8 put (_all_)(=);
9RUN;
5 Code Block
MACRO CALL
Explanation :
Verification that no system error (SYSCC) has occurred so far.
Copied!
1%mp_assert(
2 iftrue=(&syscc=0),
3 desc=Checking for error condition with special missing export,
4 outds=work.test_results
5)
6 Code Block
PROC COMPARE
Explanation :
Structural and content comparison between the original 'demo' table and the table generated from the JSON 'test'.
Copied!
1 
2PROC COMPARE base=work.demo compare=work.test;
3QUIT;
4 
7 Code Block
MACRO CALL
Explanation :
Final assertion verifying that PROC COMPARE detected no differences (SYSINFO=0).
Copied!
1%mp_assert(
2 iftrue=(&sysinfo=0),
3 desc=Returned json is identical to INPUT TABLE for all special missings,
4 outds=work.test_results
5)
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.