Published on :
Test SASHELP

Unit test of the mf_getvarlist macro

This code is also available in: Deutsch Español Français
Awaiting validation
The script performs a series of unit tests for the `mf_getvarlist` macro. It calls the macro multiple times with various configurations: without options (default lists), with a custom delimiter ('X'), with a delimiter and double quotes, and with filters for numeric or character variables. The results of each macro call are stored in macro variables. Then, a DATA step is used to compare these obtained results with expected values, and records the status of each test (PASS/FAIL) as well as explanatory comments in a SAS© table named `work.test_results`.
Data Analysis

Type : SASHELP


The script uses the `sashelp.class` table, which is a standard example table provided with SAS, as a data source for the `mf_getvarlist` macro. No external data is required, and all test data is generated or comes from SASHELP. Test results are then stored in the internal `work.test_results` table.

1 Code Block
Macro calls
Explanation :
This code block makes several calls to the `%mf_getvarlist` macro. Each call uses the `sashelp.class` table as input and varies the options for the delimiter (`dlm`) and filtering by variable type (`typefilter`). The results (variable name lists) are stored in macro variables (`test1` to `test5`) for later evaluation.
Copied!
1%let test1=%mf_getvarlist(sashelp.class);
2%let test2=%mf_getvarlist(sashelp.class,dlm=X);
3%let test3=%mf_getvarlist(sashelp.class,dlm=%str(,),quote=double);
4%let test4=%mf_getvarlist(sashelp.class,typefilter=N);
5%let test5=%mf_getvarlist(sashelp.class,typefilter=C);
2 Code Block
DATA STEP Data
Explanation :
This `DATA STEP` block creates the `work.test_results` table to record test results. For each test case, it retrieves the value of the corresponding macro variable (containing the list of variables extracted by `%mf_getvarlist`) via `symget()`. It then compares this value to a predefined `result` string that represents the expected outcome. A 'PASS' or 'FAIL' status is assigned based on this comparison, and detailed comments are recorded. The temporary variables `base` and `result` are then dropped.
Copied!
1DATA work.test_results;
2 LENGTH test_description $256 test_result $4 test_comments base RESULT $256;
3 test_description="Basic test";
4 base=symget('test1');
5 RESULT='Name Sex Age Height Weight';
6 IF base=RESULT THEN test_result='PASS';
7 ELSE test_result='FAIL';
8 test_comments="Comparing "!!trim(base)!!' vs '!!trim(RESULT);
9 OUTPUT;
10 
11 test_description="DLM test";
12 base=symget('test2');
13 RESULT='NameXSexXAgeXHeightXWeight';
14 IF base=RESULT THEN test_result='PASS';
15 ELSE test_result='FAIL';
16 test_comments="Comparing "!!trim(base)!!' vs '!!trim(RESULT);
17 OUTPUT;
18 
19 test_description="DLM + quote test";
20 base=symget('test3');
21 RESULT='"Name","Sex","Age","Height","Weight"';
22 IF base=RESULT THEN test_result='PASS';
23 ELSE test_result='FAIL';
24 test_comments="Comparing "!!trim(base)!!' vs '!!trim(RESULT);
25 OUTPUT;
26 
27 test_description="Numeric Filter";
28 base=symget('test4');
29 RESULT='Age Height Weight';
30 IF base=RESULT THEN test_result='PASS';
31 ELSE test_result='FAIL';
32 test_comments="Comparing "!!trim(base)!!' vs '!!trim(RESULT);
33 OUTPUT;
34 
35 test_description="Char Filter";
36 base=symget('test5');
37 RESULT='Name Sex';
38 IF base=RESULT THEN test_result='PASS';
39 ELSE test_result='FAIL';
40 test_comments="Comparing "!!trim(base)!!' vs '!!trim(RESULT);
41 OUTPUT;
42 
43 drop base RESULT;
44RUN;
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 use under the GNU Lesser General Public License, see the included README.md file or https://github.com/HMS-Analytical-Software/SASUnit/wiki/readme/.