Published on :
Tests CREATION_INTERNE

Column Validation Tests (mp_validatecol)

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© script executes a series of tests to evaluate the functionality of the `%mp_validatecol` macro. For each test case (validation of library/dataset names, numbers, SAS© formats, and integers), a temporary dataset is created using inline data (`datalines4`). The `%mp_validatecol` macro is then called to filter valid data according to the specified type. The `%mp_assertdsobs` macro is used after each test block to verify that the number of resulting observations matches expectations, consolidating the results in the `work.test_results` dataset.
Data Analysis

Type : CREATION_INTERNE


All data used for testing is defined directly in the script via `datalines4` blocks within `DATA STEP`.

1 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` block creates the `test1` dataset. It reads character strings from `datalines4`, assigns them to the `libds` variable, then calls the `%mp_validatecol` macro with type `LIBDS` to validate if `libds` is a valid SAS library/dataset name. Only observations where `is_libds` is 1 (valid) are kept, filtering out non-conforming entries.
Copied!
1DATA test1;
2 INFILE datalines4 dsd;
3 INPUT;
4 libds=_infile_;
5 %mp_validatecol(libds,LIBDS,is_libds)
6 IF is_libds=1;
7datalines4;
8some.LIBNAME
9!lib.blah
10%abort
11definite.ok
12not.ok!
13nineletrs._
14;;;;
15RUN;
2 Code Block
Macro (%mp_assertdsobs)
Explanation :
This macro call uses `%mp_assertdsobs` to verify that the `work.test1` dataset contains exactly 2 observations after `LIBDS` validation, confirming the expected behavior. The result of this assertion is stored in the `work.test_results` dataset.
Copied!
1%mp_assertdsobs(work.test1,
2 desc=Testing LIBDS,
3 test=EQUALS 2,
4 outds=work.test_results
5)
3 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` creates `test2` by reading strings from `datalines4` into `infile`. The `%mp_validatecol` macro is then called with type `ISNUM` to determine if each string represents a valid number. Only valid numeric observations (`is_numeric=1`) are kept in the `test2` dataset.
Copied!
1DATA test2;
2 INFILE datalines4 dsd;
3 INPUT;
4 INFILE=_infile_;
5 %mp_validatecol(INFILE,ISNUM,is_numeric)
6 IF is_numeric=1;
7datalines4;
81
90001
101e6
11-44
12above are good
13the rest are bad
14%abort
151&somethingverybad.
16&
17+-1
18;;;;
19RUN;
4 Code Block
Macro (%mp_assertdsobs)
Explanation :
This `%mp_assertdsobs` macro call verifies that the `work.test2` dataset contains exactly 4 observations after `ISNUM` validation, confirming the correct recognition of numeric values by `mp_validatecol`. The result is added to `work.test_results`.
Copied!
1%mp_assertdsobs(work.test2,
2 desc=Test2 - ISNUM,
3 test=EQUALS 4,
4 outds=work.test_results
5)
5 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` creates `test3`. It reads strings from `datalines4` into `infile` and uses `%mp_validatecol` with type `FORMAT` to identify strings that are valid SAS format names. Only observations with valid formats (`is_format=1`) are kept.
Copied!
1DATA test3;
2 INFILE datalines4 dsd;
3 INPUT;
4 INFILE=_infile_;
5 %mp_validatecol(INFILE,FORMAT,is_format)
6 IF is_format=1;
7datalines4;
8$.
9$FORMAT.
10$format12.2
11somenum.
12somenum12.4
13above are good
14the rest are bad
15%abort
161&somethingverybad.
17&
18+-1
19.
20a.A
21$format12.1b
22$format12.1b1
23;;;;
24RUN;
6 Code Block
Macro (%mp_assertdsobs)
Explanation :
This `%mp_assertdsobs` macro call validates that `work.test3` contains 5 observations, confirming that the `mp_validatecol` macro correctly identifies valid SAS formats. The result is added to `work.test_results`.
Copied!
1%mp_assertdsobs(work.test3,
2 desc=Test3 - ISFORMAT,
3 test=EQUALS 5,
4 outds=work.test_results
5)
7 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` creates `test4`, reads strings from `datalines4` into `infile`, and uses `%mp_validatecol` with type `ISINT` to check if each string represents a valid integer. Only observations that are integers (`is_integer=1`) are kept.
Copied!
1DATA test4;
2 INFILE datalines4 dsd;
3 INPUT;
4 INFILE=_infile_;
5 %mp_validatecol(INFILE,ISINT,is_integer)
6 IF is_integer=1;
7datalines4;
81
91234
10-134
11-1.0
121.0
130
14above are good
15the rest are bad
160.1
171.1
18-0.001
19%abort
201&somethingverybad.
21&
22+-1
23.
24a.A
25$format12.1b
26$format12.1b1
27;;;;
28RUN;
8 Code Block
Macro (%mp_assertdsobs)
Explanation :
This final `%mp_assertdsobs` call ensures that the `work.test4` dataset contains 6 observations, proving that `mp_validatecol` correctly identifies integer values. The result is stored in `work.test_results`.
Copied!
1%mp_assertdsobs(work.test4,
2 desc=Test4 - ISFORMAT,
3 test=EQUALS 6,
4 outds=work.test_results
5)
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/.