Published on :
Test CREATION_INTERNE

Test of the _nobs.sas macro

This code is also available in: Français Deutsch Español
Awaiting validation
This script uses the SASUnit framework to test the '_nobs.sas©' macro. It checks its functionality to count observations in an existing dataset with observations (test1), an empty dataset (test2), and a non-existent dataset (test3). Assertions are used to confirm that the macro returns the expected number of observations in each case.
Data Analysis

Type : CREATION_INTERNE


Datasets 'test1' and 'test2' are created internally within the script to simulate different test scenarios. Dataset 'test3' is intentionally non-existent to test a boundary case.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a temporary dataset named 'test1' which contains 100 observations. It serves as reference data for a standard observation count test.
Copied!
1DATA test1;
2 DO i=1 to 100;
3 OUTPUT;
4 END;
5RUN;
2 Code Block
Macro SASUnit
Explanation :
This block executes a SASUnit test case. It calls the '%_nobs' macro on the 'test1' dataset and uses '%assertEquals' to verify that the returned number of observations is 100, as expected. '%assertLog()' checks that no unexpected errors are present in the SAS log.
Copied!
1%initTestcase(i_object=_nobs.sas, i_desc=standard case with 100 obs)
2%LET g_nobs = %_nobs(test1);
3%assertEquals(i_expected=100, i_actual=&g_nobs, i_desc=number of observations must be 100)
4%assertLog()
5%endTestcase()
3 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates a temporary dataset named 'test2' which is intentionally empty (0 observations). It is used to test the behavior of the '_nobs' macro with a dataset containing no observations.
Copied!
1DATA test2;
2 stop;
3RUN;
4 Code Block
Macro SASUnit
Explanation :
This test block verifies the '%_nobs' macro with the empty 'test2' dataset. It expects the macro to return 0 observations, which is validated by '%assertEquals'.
Copied!
1%initTestcase(i_object=_nobs.sas, i_desc=dataset with 0 obs)
2%LET g_nobs = %_nobs(test2);
3%assertEquals(i_expected=0, i_actual=&g_nobs, i_desc=number of observations must be 0)
4%assertLog()
5 Code Block
Macro SASUnit
Explanation :
This test block evaluates the behavior of the '%_nobs' macro when called with a dataset ('test3') that does not exist. The '%assertEquals' assertion verifies that the macro handles this case by returning 0 observations.
Copied!
1%initTestcase(i_object=_nobs.sas, i_desc=dataset not existing)
2%LET g_nobs = %_nobs(test3);
3%assertEquals(i_expected=0, i_actual=&g_nobs, i_desc=number of observations must be 0)
4%assertLog()
5%endTestcase;
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/.