Published on :
Macro INTERNAL_CREATION

Test of the `dostuff` macro with `mp_assertscope`

This code is also available in: Deutsch Español Français
Awaiting validation
The script first defines the `dostuff` macro which accepts an `action` parameter. Depending on the value of `action` ('ADD', 'DEL', 'MOD', 'NOTHING'), the macro creates global variables, deletes them, modifies them, or does nothing. Then, the script executes a series of tests. Each test takes a 'snapshot' of the global macro variable environment with `%mp_assertscope(SNAPSHOT)`, executes `dostuff` with a specific action, then compares the new state with the initial snapshot using `%mp_assertscope(COMPARE, ...)`. Finally, `%mp_assert` is used to validate that the comparison report (stored in a work table) matches the expected result for each action (add, modify, delete, or no change).
Data Analysis

Type : INTERNAL_CREATION


The script does not read any source data. It dynamically generates work tables (work.testing_the_tester1, work.testing_the_tester2, etc.) as output of the `%mp_assertscope` test macro. These tables contain the comparison results of the macro variable environment before and after the execution of the `dostuff` macro.

1 Code Block
MACRO
Explanation :
Definition of the 'dostuff' macro. It takes an 'action' parameter and can add ('ADD'), delete ('DEL'), or modify ('MOD') global macro variables (`NEWVAR1`, `NEWVAR2`). The 'NOTHING' action declares local variables for a test of no modification to the global environment.
Copied!
1%macro dostuff(ACTION);
2 %IF &ACTION=ADD %THEN %DO;
3 %global NEWVAR1 NEWVAR2;
4 %END;
5 %ELSE %IF &ACTION=DEL %THEN %DO;
6 %symdel NEWVAR1 NEWVAR2;
7 %END;
8 %ELSE %IF &ACTION=MOD %THEN %DO;
9 %let NEWVAR1=Let us pray..;
10 %END;
11 %ELSE %IF &ACTION=NOTHING %THEN %DO;
12 %local a b c d e;
13 %END;
14%mend dostuff;
2 Code Block
MACRO Data
Explanation :
This block tests variable addition. `%mp_assertscope(SNAPSHOT)` records the initial state of global variables. `%dostuff(ADD)` is called to create `NEWVAR1` and `NEWVAR2`. `%mp_assertscope(COMPARE)` compares the final state to the snapshot and writes the differences to `work.testing_the_tester1`. `%mp_assert` verifies that the test comment in the output table correctly indicates the addition of these two variables.
Copied!
1/* check for adding variables */
2%mp_assertscope(SNAPSHOT)
3%dostuff(ADD)
4%mp_assertscope(COMPARE,outds=work.testing_the_tester1)
5%mp_assert(
6 iftrue=(
7 "%mf_getvalue(work.testing_the_tester1,test_comments)"
8 ="Mod:() Add:(NEWVAR1 NEWVAR2) Del:()"
9 ),
10 desc=Checking RESULT when vars added,
11 outds=work.test_results
12)
3 Code Block
MACRO Data
Explanation :
This block tests variable modification. After the snapshot, `%dostuff(MOD)` modifies `NEWVAR1`. The `%mp_assert` macro validates that the comparison report (in `work.testing_the_tester2`) correctly reflects the modification of `NEWVAR1`.
Copied!
1/* check for modifying variables */
2%mp_assertscope(SNAPSHOT)
3%dostuff(MOD)
4%mp_assertscope(COMPARE,outds=work.testing_the_tester2)
5%mp_assert(
6 iftrue=(
7 "%mf_getvalue(work.testing_the_tester2,test_comments)"
8 ="Mod:(NEWVAR1) Add:() Del:()"
9 ),
10 desc=Checking RESULT when vars modified,
11 outds=work.test_results
12)
4 Code Block
MACRO Data
Explanation :
This block tests variable deletion. `%dostuff(DEL)` deletes `NEWVAR1` and `NEWVAR2`. The `%mp_assert` macro verifies that the comparison report (in `work.testing_the_tester3`) indicates the deletion of these two variables.
Copied!
1/* check for deleting variables */
2%mp_assertscope(SNAPSHOT)
3%dostuff(DEL)
4%mp_assertscope(COMPARE,outds=work.testing_the_tester3)
5%mp_assert(
6 iftrue=(
7 "%mf_getvalue(work.testing_the_tester3,test_comments)"
8 ="Mod:() Add:() Del:(NEWVAR1 NEWVAR2)"
9 ),
10 desc=Checking RESULT when vars deleted,
11 outds=work.test_results
12)
5 Code Block
MACRO Data
Explanation :
This block verifies that no changes are made to global variables. `%dostuff(NOTHING)` only declares local variables. The `%mp_assert` macro confirms that the comparison report (in `work.testing_the_tester4`) indicates that global variables were not modified.
Copied!
1/* check for doing nothing */
2%mp_assertscope(SNAPSHOT)
3%dostuff(NOTHING)
4%mp_assertscope(COMPARE,outds=work.testing_the_tester4)
5%mp_assert(
6 iftrue=(
7 "%mf_getvalue(work.testing_the_tester4,test_comments)"
8 ="GLOBAL Variables Unmodified"
9 ),
10 desc=Checking results when nothing created,
11 outds=work.test_results
12)
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 : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de; Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. SPDX-License-Identifier: Apache-2.0