Published on :
Test CREATION_INTERNE

Test of the mp_base64copy macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining a simple character string, writes it to a temporary file, then encodes it in base64 using `%mp_base64copy`. The encoded result is then decoded, and the original string is compared to the decoded string using `%mp_assert`. A second series of tests is performed with a string containing multi-byte characters. This string is also encoded, then decoded, and an assertion is made to verify the integrity of the multi-byte string after the encode/decode round trip. Assertion results are stored in `work.test_results`.
Data Analysis

Type : CREATION_INTERNE


All data used (test strings) are defined and created directly within the script via macro variables or DATA steps writing to temporary files. No external data or SASHELP is directly read.

1 Code Block
DATA STEP / Macro (%mp_base64copy, %mp_assert) Data
Explanation :
This block initializes a macro variable `string1` with a simple character string. A temporary file `tmp` is created and the value of `string1` is written to it. The `%mp_base64copy` macro is then called to encode the content of `tmp` (via `inref=tmp`) and store the result in the fileref `myref`. A `data _null_` reads `myref` and displays its content in the log (for intermediate verification). The content of `myref` is then decoded by `%mp_base64copy` into `mynewref`. Finally, another `data _null_` reads `mynewref`, retrieves the decoded string and stores it in the macro variable `string1_check`. The `%mp_assert` macro is used to compare `string1` and `string1_check`, thus verifying that encoding/decoding preserved the integrity of the original string.
Copied!
1%let string1=base ik ally;
2filename tmp temp;
3DATA _null_;
4 file tmp;
5 put "&string1";
6RUN;
7%mp_base64copy(inref=tmp, outref=myref, ACTION=ENCODE)
8 
9DATA _null_;
10 INFILE myref;
11 INPUT;
12 put _infile_;
13RUN;
14%mp_base64copy(inref=myref, outref=mynewref, ACTION=DECODE)
15DATA _null_;
16 INFILE mynewref lrecl=5000;
17 INPUT;
18 put _infile_;
19 call symputx('string1_check',_infile_);
20 stop;
21RUN;
22%mp_assert(
23 iftrue=("&string1"="&string1_check"),
24 desc=Basic String Compare,
25 outds=work.test_results
26)
2 Code Block
DATA STEP / Macro (%mp_base64copy, %mp_assert) Data
Explanation :
This block tests the ability of the `%mp_base64copy` macro to handle strings containing multi-byte characters. A literal string with special characters is written to a temporary file `tmp2`. This string is then encoded in base64 into `myref2`, then decoded into `newref2` using `%mp_base64copy`. A `data _null_` reads the decoded content from `newref2` and compares it to the original string. The result of this comparison is stored in the macro variable `check2`. Finally, `%mp_assert` is used to verify that `check2` is equal to '1', confirming that the multi-byte string was correctly processed throughout the encoding and decoding process.
Copied!
1filename tmp2 temp lrecl=500;
2DATA _null_;
3 file tmp2;
4 put "'', '', '', '', '', '', '', '', '', '', '', '', '', ''";
5RUN;
6%mp_base64copy(inref=tmp2, outref=myref2, ACTION=ENCODE)
7 
8%mp_base64copy(inref=myref2, outref=newref2, ACTION=DECODE)
9DATA _null_;
10 INFILE newref2 lrecl=5000;
11 INPUT;
12 list;
13 /* do not print the string to the log else viya 3.5 throws exception */
14 IF trim(_infile_)=
15 "'', '', '', '', '', '', '', '', '', '', '', '', '', ''"
16 THEN call symputx('check2',1);
17 ELSE call symputx('check2',0);
18 stop;
19RUN;
20%mp_assert(
21 iftrue=("&check2"="1"),
22 desc=Double Byte String Compare,
23 outds=work.test_results
24)
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.