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!
%if "%substr(&sysver.XX,1,4)"="V.04" %then %do;
%put %str(ERR)OR: Viya 4 does not support the IO library in lua;
%return;
%end;
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!
%global str1;
%let file=%sysfunc(pathname(work))/file.txt;
%let pat=replace/me;
%let str=with/this;
data _null_;
file "&file";
put "&pat";
run;
1
%global str1;
2
%let file=%sysfunc(pathname(work))/file.txt;
3
%let pat=replace/me;
4
%let str=with/this;
5
DATA _null_;
6
file "&file";
7
put "&pat";
8
RUN;
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!
%mp_gsubfile(file=&file, patternvar=pat, replacevar=str)
data _null_;
infile "&file";
input;
call symputx('str1',_infile_);
run;
%mp_assert(
iftrue=("&str1"="&str"),
desc=Check that simple replacement was successful,
outds=work.test_results
)
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!
%global str2 strcheck2 strcheck2b;
%let file2=%sysfunc(pathname(work))/file2.txt;
%let pat2=replace/me;
%let str2=with/this;
data _null_;
file "&file2";
put 'line1';output;
put "&pat2";output;
put "&pat2";output;
run;
1
%global str2 strcheck2 strcheck2b;
2
%let file2=%sysfunc(pathname(work))/file2.txt;
3
%let pat2=replace/me;
4
%let str2=with/this;
5
DATA _null_;
6
file "&file2";
7
put 'line1';OUTPUT;
8
put "&pat2";OUTPUT;
9
put "&pat2";OUTPUT;
10
RUN;
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!
%mp_gsubfile(file=&file2, patternvar=pat2, replacevar=str2)
data _null_;
infile "&file2";
input;
if _n_=2 then call symputx('strcheck2',_infile_);
if _n_=3 then call symputx('strcheck2b',_infile_);
putlog _infile_;
run;
%mp_assert(
iftrue=("&strcheck2"="&str2"),
desc=Check that multi line replacement was successful (line2),
outds=work.test_results
)
%mp_assert(
iftrue=("&strcheck2b"="&str2"),
desc=Check that multi line replacement was successful (line3),
outds=work.test_results
)
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!
%gsubtest()
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.