Published on :
Test SASHELP

Test of the _existVar.sas macro

This code is also available in: Français Deutsch Español
The script is a unit test implemented with the SASUnit framework. It contains several test cases to evaluate the behavior of the `_existVar.sas©` macro. Test cases include:
1. Checking the existence of the 'age' variable as numeric in the 'sashelp.class' dataset.
2. Checking that the 'age' variable is not of character type in 'sashelp.class'.
3. Checking the existence of the 'name' variable (without type specification) in 'sashelp.class'.
Each test uses the SASUnit `%assertEquals` macro to compare the result obtained from `_existVar` with an expected value (1 for true, 0 for false), thus ensuring the macro's validity.
Data Analysis

Type : SASHELP


The script uses the 'class' dataset from the SASHELP library. SASHELP is a standard SAS library, provided with any SAS installation, and therefore does not require external data or script-specific data creation for its execution.

1 Code Block
MACRO CALL
Explanation :
Initializes a new SASUnit test scenario. This sets the global context for a series of related tests, facilitating the organization and reporting of test results.
Copied!
1%initScenario (i_desc=Test of _existVar.sas);
2 Code Block
MACRO CALL
Explanation :
This block defines a test case to check for the presence of a numeric variable named 'age' in 'sashelp.class'. The `_existVar` macro is called with 'N' to specify a numeric type. The result is stored in `&rc.` and then compared to 1 (true) to confirm that the numeric variable 'age' indeed exists.
Copied!
1%initTestcase(i_object=_existVar.sas, i_desc=Test for numeric variable age)
2%let rc=%_existVar (sashelp.class
3 ,age
4 ,N
5 );
6%endTestcall;
7 
8%assertEquals(i_expected=1, i_actual=&rc., i_desc=Numeric variable age exists)
9%endTestcase;
3 Code Block
MACRO CALL
Explanation :
This test case verifies that the 'age' variable is not recognized as a character type variable in 'sashelp.class'. The `_existVar` macro is called with 'C' for the character type. The expected result is 0 (false), confirming that 'age' is not a character variable, which is correct given that it is numeric in 'sashelp.class'.
Copied!
1%initTestcase(i_object=_existVar.sas, i_desc=Test for character variable age)
2%let rc=%_existVar (sashelp.class
3 ,age
4 ,C
5 );
6%endTestcall;
7 
8%assertEquals(i_expected=0, i_actual=&rc., i_desc=Character variable age does not exist)
9%endTestcase;
4 Code Block
MACRO CALL
Explanation :
This block tests the existence of the 'name' variable in 'sashelp.class' without specifying a type. The `_existVar` macro is called without the type parameter. The expected result is 1 (true), which validates that the 'name' variable exists in the 'sashelp.class' dataset.
Copied!
1%initTestcase(i_object=_existVar.sas, i_desc=Test for variable name)
2%let rc=%_existVar (sashelp.class
3 ,name
4 );
5%endTestcall;
6 
7%assertEquals(i_expected=1, i_actual=&rc., i_desc=Variable name does not exist)
8%endTestcase;
5 Code Block
MACRO CALL
Explanation :
Marks the end of the SASUnit test scenario. This means that all test cases associated with this scenario have been executed and the results can be compiled.
Copied!
1%endScenario();
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 This file is part of SASUnit, the Unit testing framework for SAS(R) programs. For copyright information and terms of usage under the GNU Lesser General Public License see included file README.md or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.