Published on :
Test, Macro INTERNAL_CREATION

Test of the mp_chop macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by defining macro variables for temporary file paths and the matching string. It then creates a temporary text file (`file.txt`) in the WORK library with predefined content. The `%mp_chop` macro is called four times, each call testing a specific combination of parameters to split the input file and save the results into distinct output files. The `%mp_assertscope` macros are used for test context management. After `%mp_chop` execution, the script reads the content of each output file into macro variables and uses the `%mp_assert` macro (part of the SASUnit framework) to validate that the splitting results match expectations for each test scenario.
Data Analysis

Type : INTERNAL_CREATION


The input data (`file.txt`) is entirely created and managed within the script itself, in the temporary WORK library, via a DATA _NULL_ step. The output files produced by %mp_chop are also temporary, and their contents are read internally for test verification.

1 Code Block
Macro-variable definition
Explanation :
This block initializes macro variables essential to the script, defining the temporary input file name, the character string to search for, and the names of the four output files that will contain the results of the %mp_chop macro.
Copied!
1%let src="%sysfunc(pathname(work))/file.txt";
2%let str=Chop here!;
3%let out1="%sysfunc(pathname(work))/file1.txt";
4%let out2="%sysfunc(pathname(work))/file2.txt";
5%let out3="%sysfunc(pathname(work))/file3.txt";
6%let out4="%sysfunc(pathname(work))/file4.txt";
2 Code Block
DATA STEP Data
Explanation :
Creates a temporary text file (`file.txt`) in the WORK library. This file serves as the input dataset for testing the %mp_chop macro, with simple content to verify splitting operations.
Copied!
1DATA _null_;
2 file &src;
3 put "startsection&str.endsection";
4RUN;
3 Code Block
Macro call Data
Explanation :
This block executes the %mp_chop macro four times, testing different combinations of parameters (retaining the 'FIRST' or 'LAST' part, and 'START' or 'END' match point). Each call writes the result to a distinct temporary file. `%mp_assertscope` is used to manage the test environment, probably to save and restore the state of macro variables.
Copied!
1%mp_assertscope(SNAPSHOT)
2%mp_chop(&src, matchvar=str, keep=FIRST, outfile=&out1)
3%mp_chop(&src, matchvar=str, keep=LAST, outfile=&out2)
4%mp_chop(&src, matchvar=str, keep=FIRST, matchpoint=END, outfile=&out3)
5%mp_chop(&src, matchvar=str, keep=LAST, matchpoint=END, outfile=&out4)
6%mp_assertscope(COMPARE)
4 Code Block
DATA STEP
Explanation :
This series of DATA _NULL_ steps reads the content of each of the four output files generated by %mp_chop and stores the first line of each file in dedicated macro variables (`test1` to `test4`). These macro variables will be used for result verification.
Copied!
1DATA _null_;
2 INFILE &out1 lrecl=200;
3 INPUT;
4 call symputx('test1',_infile_);
5DATA _null_;
6 INFILE &out2 lrecl=200;
7 INPUT;
8 call symputx('test2',_infile_);
9DATA _null_;
10 INFILE &out3 lrecl=200;
11 INPUT;
12 call symputx('test3',_infile_);
13DATA _null_;
14 INFILE &out4 lrecl=200;
15 INPUT;
16 call symputx('test4',_infile_);
17RUN;
5 Code Block
Macro call
Explanation :
This block uses the `%mp_assert` macro to perform four distinct assertions. Each assertion checks if the content of a macro variable (derived from the output files of %mp_chop) matches the expected character string. The descriptions (`desc`) and the output dataset for the test results (`outds=work.test_results`) are specified for each assertion, documenting the different test scenarios.
Copied!
1%mp_assert(
2 iftrue=("&test1" = "startsection"),
3 desc=Checking keep FIRST matchpoint START
4 outds=work.test_results
5)
6%mp_assert(
7 iftrue=("&test2" = "Chop here!endsection"),
8 desc=Checking keep LAST matchpoint START
9 outds=work.test_results
10)
11%mp_assert(
12 iftrue=("&test3" = "startsectionChop here!"),
13 desc=Checking keep FIRST matchpoint END
14 outds=work.test_results
15)
16%mp_assert(
17 iftrue=("&test4" = "endsection"),
18 desc=Checking keep LAST matchpoint END
19 outds=work.test_results
20)
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.
Copyright Info : The script references and uses macros from the SASUnit framework. SASUnit copyright is: Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de. This file is part of SASUnit, the unit testing framework for SAS(R) programs.