Published on :
Tests CREATION_INTERNE

Test macro for mp_gsubfile

This code is also available in: Deutsch Español Français
Awaiting validation
The `gsubtest` macro begins with a conditional SAS© version check. If execution is on SAS© Viya 4, the macro displays an error message and stops, as the Lua IO library, required by `mp_gsubfile`, is not supported in this environment. It then proceeds with two distinct tests: a simple pattern replacement in a temporary file and a multiple pattern replacement across different lines of another temporary file. For each scenario, test files are created, the `mp_gsubfile` macro is called, and the results are validated using the `mp_assert` macro. Assertion results are recorded in the `work.test_results` dataset. This approach ensures the reliability of the `mp_gsubfile` utility for file manipulation.
Data Analysis

Type : CREATION_INTERNE


All data used is generated dynamically and internally by the script. These are primarily temporary text files created in the SAS WORK library to simulate text replacement scenarios. No data from SASHELP or unmanaged external sources is necessary for these tests to run.

1 Code Block
Macro Conditionnelle
Explanation :
This code block performs an environment check. It tests if the SAS version is 'V.04', corresponding to SAS Viya 4. If so, an error message is displayed in the SAS log, and the macro execution is interrupted. This indicates an incompatibility of the Lua IO library, essential for `mp_gsubfile`, with SAS Viya 4 at this stage.
Copied!
1%IF "%substr(&sysver.XX,1,4)"="V.04" %THEN %DO;
2 %put %str(ERR)OR: Viya 4 does not support the IO library in lua;
3 %return;
4%END;
2 Code Block
DATA STEP Data
Explanation :
This block prepares the first test scenario. It declares a global macro variable `str1`, defines the path of a temporary file `file.txt` in the `WORK` library via `pathname(work)`, and initializes the search (`pat`) and replacement (`str`) patterns. A `DATA STEP _null_` is used to create the `file.txt` and write the initial string 'replace/me' to it, which will be the target for replacement.
Copied!
1%global str1;
2%let file=%sysfunc(pathname(work))/file.txt;
3%let pat=replace/me;
4%let str=with/this;
5DATA _null_;
6 file "&file";
7 put "&pat";
8RUN;
3 Code Block
Macro Appel
Explanation :
In this block, the `mp_gsubfile` macro is called to replace the pattern `&pat` with `&str` in the file `&file`. Then, a `DATA STEP _null_` rereads the modified content of `&file`, and the first line is stored in the macro variable `str1`. Finally, the `mp_assert` macro is used to verify that the replacement was performed correctly by comparing `&str1` with the expected value `&str`. The assertion result is recorded in the `work.test_results` dataset.
Copied!
1%mp_gsubfile(file=&file, patternvar=pat, replacevar=str)
2DATA _null_;
3 INFILE "&file";
4 INPUT;
5 call symputx('str1',_infile_);
6RUN;
7 
8%mp_assert(
9 iftrue=("&str1"="&str"),
10 desc=Check that SIMPLE replacement was successful,
11 outds=work.test_results
12)
4 Code Block
DATA STEP Data
Explanation :
This block prepares the second test scenario, focusing on multi-line replacement. It initializes new global macro variables (`str2`, `strcheck2`, `strcheck2b`), defines a second temporary file `file2.txt` and its search (`pat2`) and replacement (`str2`) patterns. A `DATA STEP _null_` creates `file2.txt` and writes three lines to it: 'line1' followed by two occurrences of 'replace/me', simulating a file with multiple replacement targets.
Copied!
1%global str2 strcheck2 strcheck2b;
2%let file2=%sysfunc(pathname(work))/file2.txt;
3%let pat2=replace/me;
4%let str2=with/this;
5DATA _null_;
6 file "&file2";
7 put 'line1';OUTPUT;
8 put "&pat2";OUTPUT;
9 put "&pat2";OUTPUT;
10RUN;
5 Code Block
Macro Appel
Explanation :
In this block, the `mp_gsubfile` macro is called to replace patterns in `&file2`. A `DATA STEP _null_` then reads the modified content of `&file2`. Specific lines (2 and 3) where replacements should have occurred are extracted and stored in macro variables `strcheck2` and `strcheck2b`. Two calls to the `mp_assert` macro are made to independently verify if the replacements on these lines were successful, thus validating `mp_gsubfile`'s multi-line capability. The results are added to `work.test_results`.
Copied!
1%mp_gsubfile(file=&file2, patternvar=pat2, replacevar=str2)
2DATA _null_;
3 INFILE "&file2";
4 INPUT;
5 IF _n_=2 THEN call symputx('strcheck2',_infile_);
6 IF _n_=3 THEN call symputx('strcheck2b',_infile_);
7 putlog _infile_;
8RUN;
9 
10%mp_assert(
11 iftrue=("&strcheck2"="&str2"),
12 desc=Check that multi line replacement was successful (line2),
13 outds=work.test_results
14)
15%mp_assert(
16 iftrue=("&strcheck2b"="&str2"),
17 desc=Check that multi line replacement was successful (line3),
18 outds=work.test_results
19)
6 Code Block
Macro Appel
Explanation :
This block represents the final call to the `gsubtest()` macro. This invocation triggers the sequential execution of all test blocks and compatibility checks previously defined within the macro, marking the beginning of the effective test execution.
Copied!
1%gsubtest()
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.